NameSilo

PHP File Upload error

Spaceship Spaceship
Watch
Impact
111
Hi,

I'm getting an error with this file upload. My form tag has that enctype="multipart/formdata" in it, and input type="file", yada yada yada... all been set up, but here's the code:

PHP:
//tmp location.
copy ($_FILES['file']['tmp_name'], $_FILES['file']['name']) or die ('Could not upload');

if ($file != "")
{

$file = $HTTP_POST_FILES['filename']['tmp_name'];
$file_name = $HTTP_POST_FILES['filename']['name'];
$file_size = $HTTP_POST_FILES['filename']['size'];
$file_type = $HTTP_POST_FILES['filename']['type'];
$file_error = $HTTP_POST_FILES['filename']['error'];

if ($file_error > 0)
{
	echo "Problem: ";
	switch ($file_error)
	{
		case 1: display_error('File is too big. Max size is 3 MB.'); break;
		case 2: display_error('File is too big. Max size is 3 MB.'); break;
		case 3: display_error('The file was only partially uploaded.'); break;
		case 4: display_error('The file did not upload.'); break;
	}
	exit;
}

if ($file_type != "zip")
{
	echo(' >>> '.$file_type.' <<< ');
	display_error('That is not a valid file. File extention must be .zip.');
}

if ($file_size > $MAX_FILE_SIZE)
{
	display_error('File is too large - maximum size allowed is 3 MB.');
}

//put the file where we want it.
$location = '/home/***/public_html/flonds/**/**/**/**/**/'.$file_name;

if (is_uploaded_file($filename))
{
	if (!move_uploaded_file($filename, $upfile))
	{
		display_error('Could not move file to directory.');
	}
}
else
{
	display_error('Problem: Possible file upload attack. Filename: '.$file_name);
}
}

I have the pathname scrambled just for privacy's sake.

My idea was to try copying it to a temporary location (2nd line) but that doesn't work, and I get the error as early in the script as that.

Any help appreciated. Thanks. (This is my first PHP file upload)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
are you using php 4.1.0 or newer if so use $_FILES instead of $HTTP_POST_FILES

Also when copying the file where are you trying to copy it too? ensure you use a proper server path so that the script knows EXACTLY where to copy it, also ensure you have write permissions for that directory (ie chmod the directory). There is no need to copy the temp file only to have to move it elsewhere anyway, this is just creating extra work. The file should not delete from the temp folder until the script is terminated.
 
Last edited:
0
•••
Yes, I'm using PHP 4.3... but does it really MATTER? If so, I will use the new form... that you suggest.

I am sure the server path is correct, CHMOD is correct, etc... etc...
 
0
•••
the $HTTP_etc global variables are deprecated ie in a future version they may not exist, $_FILES will be around long after $HTTP_POST_FILES

in the following line:-
PHP:
copy ($_FILES['file']['tmp_name'], $_FILES['file']['name']) or die ('Could not upload');

where are you intending to copy this file? why copy it in the first place you are moving it later anyway?
 
0
•••
I dunno... I was just trying stuff...
 
0
•••
can u post your html form and copy all errors you see.
 
0
•••
PHP:
	if (isSet($_FILES['file'])) {
		if (is_uploaded_file($_FILES['file']['tmp_name'])) {
			if ($_FILES['file']['error'] > 0) {
				switch ($_FILES['file']['error'])
				{
					case 1:
						exit('File is too big. Max size is 3 MB.');
						break;
					case 2:
						exit('File is too big. Max size is 3 MB.');
						break;
					case 3:
						exit('The file was only partially uploaded.');
						break;
					case 4:
						exit('The file did not upload.');
						break;
				}
			}
			if ($_FILES['file']['type'] != 'zip') {
				exit($_FILES['file']['type'] . ' is not a valid extension. File extention must be .zip.');
			}
			if ($_FILES['file']['size'] > '3072') {
				exit('File is too large - maximum size allowed is 3 MB.');
			}

			$location = '/home/***/public_html/flonds/**/**/**/**/**/' . $_FILES['file']['name'];

			if (move_uploaded_file($_FILES['file']['tmp_name'], $location)) {
				echo 'Upload Successful';
			} else {
				exit('Could Not Move Uploaded File');
			}
		} else {
			exit('File Not Uploaded');
		}
	}
 
0
•••
Thanks, Icespadez! It works!

Although, the filesize '3072' is not 3 MB - that's 3.072 KB lol or 30.72 O.o... but I fixed that ;) Thanks!!!
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back