Page 1 of 1

File Upload Error

Posted: Mon Dec 12, 2011 5:59 pm
by rawrstin
Hey, I'm trying to make a form that uploads a file and then logs some information about the file into a MYSQL database. Except, I can't even upload the file.
if (isset($_POST['add_form'])) {
		if (!is_dir("Forms")) {
			mkdir("Forms");
		} else {
			if (isset($_FILES['file'])) {
				$dir = "Forms/{$_FILES['file']['name']}";	
				if (move_uploaded_file($_FILES['file']['tmp_name'], $dir)) {
					add_form($_POST['upload_date'], $_POST['form_name'], $dir);
				} else {
					if (copy($_FILES['file']['tmp_name'], $dir)) {
						add_form($_POST['upload_date'], $_POST['form_name'], $dir);
					} else print "YOU GOT AN ERROR.";
				}
			} else if (isset($_POST['form_url'])) {
				add_form($_POST['upload_date'], $_POST['form_name'], $_POST['form_url']);	
			}
		}
	}
I ALWAYS end up in the "YOU GOT AN ERROR." part.

I've done everything I can to make this work, but I just don't know anymore.

The form does have "enctype="multipart/form-data"" in case you're wondering.

Thanks,
rawrstin

Re: File Upload Error

Posted: Mon Dec 12, 2011 9:18 pm
by jacek
It's probably a write permission error, the code looks okay.

You can remove the copy() part though, if move_uploaded_files() fails for some reason then copy will also fail for the same reason. Plus copy does not make sure that the file being copied has actually been uploaded so it might open up a security issue.

Removing that leaves you with
if (move_uploaded_file($_FILES['file']['tmp_name'], $dir)) {
                                        add_form($_POST['upload_date'], $_POST['form_name'], $dir);
                                } else {
                                        print "YOU GOT AN ERROR.";
                                }
So you need to find out what that error is, to do that you can print out the value of the error contant
echo $_FILES['file']['error'];
which will give you a number, you then need to compare that number to the ones on this page http://php.net/manual/en/features.file- ... errors.php to work out why it's failing.

If the error code does not make the problem obvious, report back :D

Re: File Upload Error

Posted: Mon Dec 12, 2011 11:42 pm
by rawrstin
Jacek,
Thank you for the help. Although, I'm still not able to upload the file. My error code is 0, which supposedly means that the file uploaded, but it doesn't. You said something about Write permissions. How do I tamper with those settings?

Thanks

Re: File Upload Error

Posted: Tue Dec 13, 2011 10:41 pm
by jacek
You would normally get an error telling you that permission to write to the folder was denied so it can't be that, unless you don;t have error_reporting set to E_ALL ?