开发者

Uploading in php

I'm going through the O'Reilly book Learning PHP & MySQL 2nd ed. by Michele Davis & Jon Phillips.

I'm stuck on example 11-28. The goal is to upload a picture and move it from /tmp to an uploads folder if it meets certain conditions: file size, type, and whether or not it was uploaded. This is being stored on my home Ubuntu server.

The form is here, and the code is here. Having a hard time getting it to show up.

When I hit the submit button w/o there being a file in the file box, I get the following warning:

Warning: unlink() [function.unlink]: No such file or directory in /home/luna/public_html/learn_php/up_urs.php on line 10

When I submit a file bigger than maxsize I get my error message:

Error. File must be less than 28480 bytes.

When I submit a file of the wrong type I get my error message:

You may only upload .gif & .jpg files.

When I submit a file that's the right type & size I get the following warning and my OK message:

Warning: move_uploaded_file(learn_php/uploads/) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/luna/public_html/learn_php/up_urs.php on line 21

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php4Mhykl' to 'learn_php/uploads/' in /home/luna/public_html/learn_php/up_urs.php on line 21 Thanks for your upload.

What I've tried:

  • I thought the problem was permissions but changing the directory learn_php to 777 made no difference so I changed it back to 755.
  • Changing the path from "uploads" to /home/luna/public_html/learn_php/uploads
  • Using getcwd() befor开发者_开发技巧e & after the unlink & move_uploaded_file commands but that only told me I was in learn_php...
  • I tried uncommenting //print_r($_FILES); but that didn't show me anything after pressing submit.

Not sure what to look for now. Thanks for any help :)


The first error you see is because you are trying to unlink a file that doesn't exist on line 10. After you do the is_uploaded_file() check you should do a quick if ( isset($_FILES['upload_file']['tmp_name']) ) before doing the unlink.

The last two warnings are because it seems you don't have the upload directory already created. I don't think move_uploaded_file() will create it for you. So just create that directory and make sure that it has proper permissions to be written to.

As a beginner, I would also recommend checking the return values of these functions so that you don't return 'all good' when things have actually failed for some reason. Start putting in code like if ( !move_uploaded_file($_FILES['upload_file']['tmp_name'], "/path/to/learn_php/uploads/".$_files['upload_file']['name']) ) { $error = "Could not move the file to the uploads directory."; } else { print "All good, thanks for the upload."; exit(); }


See my answer here. That will give you a function to find the current root directory, which you can use as a prefix for file uploads and image manipulation.

Then, before running unlink, do an is_file() check first (and before moving it, you could run it to to ensure it exists in tmp if you are paranoid)


Rather than guessing your code path try working down from the top.

$uploadpath=$_SERVER['DOCUMENT_ROOT'].'\learn_php\uploads\';

I might be wrong, since you didnt post your source, but you are missing a slash in front of a directory, so it may be trying to store it in \learn_php\learn_php\uploads\ which wont exist, since the PHP file is already in that directory, so it is attempting to access a sub directory.

if all else fails, you will probably want to brush up on the docs.

http://php.net/manual/en/features.file-upload.php


Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php4Mhykl' to 'learn_php/uploads/' in /home/luna/public_html/learn_php/up_urs.php on line 21

This is telling you that the folder you are trying to move to (learn_php/uploads/) doesn't exist. Double check that you've created that folder on your server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜