Forum Moderators: coopster

Message Too Old, No Replies

PHP Upload Script

Yet Another Question

         

armsja

3:23 am on Sep 14, 2005 (gmt 0)



I am trying to use the script below to upload "jpeg ,gif ,avi ,mpg. I can get it to upload the gifs, jpegs, txt files just fine.

I've edited my php.ini file to allow the maxupload huge, to allow people to upload large movies and such.

I try to upload movies (mpg) the status bar goes across the bottom of my browser and boom nothing, no error no nothing. I check the upload directory and no .mpg files are listed there. I know some of you have seen this upload script before and have helped out others in the past so hopefully this should be easy.

Should I be using something else like a FTP script for uploading large files, just under a gig each?
If so can you direct me to a nice php ftp script that is not fancy and works with minimal time and troubleshooting?

I am not so sure about those mime types, any suggestions or are they looking O.K.

Environment:
Unix / php4 / apache

<?php
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// You may change maxsize, and allowable upload file types.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Mmaximum file size. You may increase or decrease.
$MAX_SIZE = 200000000000;

//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('application/pdf','video/mpg','video/avi','image/jpeg','image/jpg','image/gif'
,'image/png','application/msword');

//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('.pdf','.txt','.zip','.jpg','.png','.gif','.avi','.mpg','.mpeg','.asf');

//Allow file delete? no, if only allow upload only
$DELETABLE = false;

//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Do not touch the below if you are not confident.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/************************************************************
* Setup variables
************************************************************/
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir = "files/";
$upload_url = $url_dir."/files/";
$message ="";

/************************************************************
* Create Upload Directory
************************************************************/
if (!is_dir("files")) {
if (!mkdir($upload_dir))
die ("upload_files directory doesn't exist and creation failed");
if (!chmod($upload_dir,0755))
die ("change permission to 755 failed.");
}

/************************************************************
* Process User's Request
************************************************************/
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
fclose($resource);

if (strpos($_REQUEST[del],"/.")>0); //possible hacking
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this?message=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resource);

$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));

//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Type/Extension Check
else if (!in_array($file_type, $FILE_MIMES)
&&!in_array($file_ext, $FILE_EXTS) )
$message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
else
$message = do_upload($upload_dir, $upload_url);

print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";

/************************************************************
* List Files
************************************************************/
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) &&!is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a>";
if ($DELETABLE) {
$delfile = $file;
$delfile = str_replace("%","%25",$delfile);
$delfile = str_replace("&","%26",$delfile);
$delfile = str_replace("+","%2b",$delfile);
$delfile = str_replace("?","%3f",$delfile);
$filelist .= " <a href='?del=$upload_dir".$delfile."' title='delete'>x</a>";
}
$filelist .= "<sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;

//File Name Check
if ( $file_name =="") {
$message = "Invalid File Name Specified";
return $message;
}

$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"$file_name uploaded successfully." :
"Somthing is wrong with uploading a file.";
return $message;
}

?>

<center>
<font color=red><?=$_REQUEST[message]?></font>
<br>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>

<br><b>Upload area</b>
<hr width=70%>
<?=$filelist?>
<hr width=70%>
<small><sup>Maintained by "Me"
<a style="text-decoration:none" href="http://URL-HERE/forum">Back</a>
</sup></small>
</center>

coopster

10:53 pm on Sep 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, armsja.

Well, just under a gig each is certainly a large file to be uploading, no doubt. You might look into PHP's FTP Functions [php.net] or fsockopen() [php.net].

Matt the Hoople

7:33 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



Uploading huge files via http is pretty hit or miss. IME, if you keep the files to <= 500k, you shouldn't have trouble. Anything over that gets dicey as the file size increases. The webserver is more likely than not to time out. As suggested - look into ftp or fsockopen().

vdoyl

9:12 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



Hi,

Check your php.ini again.

max_execution_time
post_max_size
upload_max_filesize