• 0

[PHP] File upload problem


Question

Hello, I'm trying to make a very basic upload script. Everything is working fine, except when I try to move the uploaded files from tmp_dir to destination. Here's the code I have:

<?php
	$max_filesize = 3 * 1024 * 1024;
	$allowed_extensions = array ("jpeg", "doc", "pdf", "xls");

	//print_r($_SERVER);
	for ($i = 0; $i < 4; $i++)
	{
		//error == 4 means the user is trying to upload an empty field.
		if ($_FILES['file'.$i]['error'] == 4) {
			echo "Skipping file #", $i, ", because it's empty";
			continue;
		}
		else {
			//storing filesize
			$filesize = $_FILES['file'.$i]['size'];

			//getting the filename and the extension.
			list ($filename, $extension) = explode(".", $_FILES['file'.$i]['name']);

			if ($filesize > $max_filesize) {
				echo "The file size is way too big, skipping file #", $i;
				continue;
			}
			else {
				if (strcmp($extension, $allowed_extensions[$i]) != 0) {
					echo "File extension is invalid, skipping file #", $i;
					continue;
				}
				else {
					//echo substr(base_convert(fileperms($_FILES['file'.$i]['tmp_name']), 10, 8), 3);
					//chown($_FILES['file'.$i]['tmp_name'], "THEODOR");

					if(mkdir ($filename)) {
						move_uploaded_file($_FILES['file'.$i]['tmp_name'], dirname($_SERVER['PHP_SELF'])."/".$filename);
					}
					else {
						echo "failed to create folder";
					}
				}
			 }#endif
		}#end if
	}#end forloop.
?>

Error:

  Quote
Warning: move_uploaded_file(/Project/jpeg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\uploader_.php on line 34

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\php2B9.tmp' to '/Project/jpeg' in C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\uploader_.php on line 34

Warning: move_uploaded_file(/Project/doc) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\uploader_.php on line 34

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\php2BA.tmp' to '/Project/doc' in C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\uploader_.php on line 34

Warning: move_uploaded_file(/Project/pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\uploader_.php on line 34

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\php2BB.tmp' to '/Project/pdf' in C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\uploader_.php on line 34

Warning: move_uploaded_file(/Project/excel) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\uploader_.php on line 34

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\php2BC.tmp' to '/Project/excel' in C:\Documents and Settings\Theodor\Desktop\wwwroot\Project\uploader_.php on line 34

Any ideas?

Link to comment
https://www.neowin.net/forum/topic/632007-php-file-upload-problem/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I might be completely wrong there, but I think your problem is that dirname($_SERVER['PHP_SELF']) is returning a relative path to your DOCUMENT_ROOT which is being treated as an absolute path by move_uploaded_file. Instead of using $_SERVER['PHP_SELF'] try using $_SERVER['SCRIPT_FILENAME'] instead. This will give you the absolute path to the PHP script including drive letter which might work better.

And call this idle curiosity if you must, are you aware your code is restricting file extensions by upload field rather than testing each uploaded file against the list of allowed file types? As you have it now you can only use the first field to upload .jpeg files, second field to upload .doc, third .pdf and forth .xls. This means if you wanted to upload 4 jpeg images but no PDF files you would need to upload them one at a time using the first field only, likewise if you wanted to upload 4 pdf files you would have to upload each one separately but using only the 3rd field.

This might be deliberate, but if it's not I just thought you'd want to know, if you hadn't noticed already :)

  • 0

I had a whole load of text here, but as I went on I kept finding more and more things wrong. My advice: every now and then it's a good idea to just scrap the lot and start fresh. Try to keep in mind your directory paths and the differences between how they work on windows and linux this time though.

Read the user notes on http://php.net/move_uploaded_file and related pages and you'll be fine.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.