Hi all.
I'm trying to use the upload function but incorporate a size limit as well. Unfortunately, the code I am using seems to have an error somewhere that causes it to always state that the file is too big to be uploaded.
Here is my code:
<?
$directory = "./uploads";
$max_filesize = 2000000;
function upload_form() {
global $PHP_SELF;
?>
<FORM METHOD="POST" ENCTYPE="MULTIPART/FORM-DATA"
ACTION="<? echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="upload">
Upload file!
<INPUT TYPE="FILE" NAME="userfile">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="upload">
</FORM>
<?
}
function upload_file() {
global $userfile, $userfile_name, $userfile_size,
$userfile_type, $directory, $WINDIR;
if(isset($WINDIR)) $userfile = str_replace("\\\\","\\", $userfile);
$filename = basename($userfile_name);
if($userfile_size > $max_filesize) echo "Error: $filename is too big. " . number_format($max_filesize) . " bytes is the limit.";
else if(!copy($userfile, "$directory/$filename")) echo "Error: $filename cannot be copied.";
else echo "successfully uploaded $filename.";
}
?>
<HTML>
<HEAD><TITLE>Upload photo</TITLE></HEAD>
<BODY>
<?
if($action == 'upload') upload_file();
else upload_form();
?>
</BODY>
</HTML>
A "working" example can be seen here: http://www.2312.co.uk/teentroubles/uploadphoto.php
I believe it is to do with this section of code but I can't for the life of me figure out what:
if($userfile_size > $max_filesize) echo "Error: $filename is too big. " . number_format($max_filesize) . " bytes is the limit.";
else if(!copy($userfile, "$directory/$filename")) echo "Error: $filename cannot be copied.";
else echo "successfully uploaded $filename.";
Thanks for any help you can offer.