View Full Version : Checking if a file is uploaded in PHP
loxinthe
January 2nd, 2008, 11:37
Ok I have this bit of code that assigns one value to a variable if the file upload field on the previous file is populated, and a different one if it isn't. It always have the value of $target. help Please.
if(isset($_FILES['pic'])) {
$target = "specialsimages/";
$target = $target . basename(_FILES['pic']['name']);
move_uploaded_file($_FILES['pic']['tmp_name'], $target);
$pic=$target;
}else{
$pic=$_POST['pico'];
}
Dan
January 3rd, 2008, 20:45
You forgot a $ on line four.. besides that, are you seeing any errors?
loxinthe
January 4th, 2008, 02:38
No I don't see any errors, and I have echoed $target, $pic, and $pico to see the value after this statement. no matter what, $pic always equals $target.
as far as the missing $ on line four before _FILES, I have it in my actual script, and don't understand why it is missing here.
inimino
January 4th, 2008, 04:17
Check the documentation for PHP's file upload feature:
http://www.php.net/manual/en/features.file-upload.php
"How do I tell if a file was uploaded" is a common question and is answered in detail on that page.
loxinthe
January 4th, 2008, 05:55
Thanks for the tip. I read the information and got all of it, but it doesn't help in this case. I know how to handle file uploads, what I need is how to accurately check if the user filled out the file field of the previous form or not.
Great link though for any that need to know how to handle file uploads.
inimino
January 4th, 2008, 06:50
I read the information and got all of it,
I think you missed the part that tells you how to tell if any file was uploaded.
Your code "if(isset($_FILES['pic']))" is not correct, but the correct way is in fact described on the page I pointed you to.
I know how to handle file uploads, what I need is how to accurately check if the user filled out the file field of the previous form or not.
Please look again at the page I pointed you to. Note the sentence before example #3 on that page which begins, "If no file is selected for upload in your form," and which tells you exactly what to check.
loxinthe
January 4th, 2008, 23:03
Awesome. That solved it thanks. I am sorry I didn't read thoroughly. I looked at the examples and assumed the text before and after only described what was in the example. My apologies.
Thanks a ton
:oops:
inimino
January 5th, 2008, 06:37
You're welcome! Glad you got it working. :)
vBulletin® v3.6.8, Copyright ©2000-2012, Jelsoft Enterprises Ltd.