File Upload Error

Ask about a PHP problem here.
Post Reply
rawrstin
Posts: 4
Joined: Fri Sep 30, 2011 11:42 pm

File Upload Error

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: File Upload Error

Post 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
Image
rawrstin
Posts: 4
Joined: Fri Sep 30, 2011 11:42 pm

Re: File Upload Error

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: File Upload Error

Post 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 ?
Image
Post Reply