noclist Posted February 14, 2011 Share Posted February 14, 2011 How do I determine if $_FILES['thumbNail']['name'] is blank? I am uploading images into a folder on my server called thumbNail and I would like to specify a default image (noimage.gif) if no prior image has been uploaded, but I'm having trouble getting an if statement to register true if the image upload was skipped. Thank you. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2011 Share Posted February 14, 2011 First, isset(). Then you can use strlen() if it's a string to see how many characters long it is. Quote Link to comment Share on other sites More sharing options...
noclist Posted February 14, 2011 Author Share Posted February 14, 2011 if (!isset($_FILES['thumbNail']['name'])) { $thumbNail="thumbNail/noimage.gif"; } Is something like that how I would check if there is a value already? Then obviously I'm trying to assign noimage.gif if there is no value. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2011 Share Posted February 14, 2011 if (!isset($_FILES['thumbNail']['name'])) { $thumbNail="thumbNail/noimage.gif"; } Is something like that how I would check if there is a value already? Then obviously I'm trying to assign noimage.gif if there is no value. Yes, but to make it make sense you need to do this if (!isset($_FILES['thumbNail']['name'])) { $thumbNail="thumbNail/noimage.gif"; }else{ $thumbNail=$_FILES['thumbNail']['name']; } So you keep using the same variable. (I'd switch the order of the if/else and use isset() instead of !isset() personally) Quote Link to comment Share on other sites More sharing options...
noclist Posted February 14, 2011 Author Share Posted February 14, 2011 Okay, looks like it is verifying correctly, but I'm still having trouble assigning the default image. It turns out the folder I was uploading to is called movieThumbs, not thumbNail... if (isset($_FILES['thumbNail']['name'])) { $thumbNail = $_FILES['thumbNail']['name']; } else { $thumbNail="movieThumbs/noimage.gif"; } Leaving the thumbnail field blank still results in a broken image linking to just the movieThumbs/ directory. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2011 Share Posted February 14, 2011 don't just use isset(), also use strlen(), as I originally posted. You need to check if isset and that it's more than 0 characters. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 14, 2011 Share Posted February 14, 2011 I'd also recommend throwing trim() into the mix. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 14, 2011 Share Posted February 14, 2011 Your code needs to test the $_FILES['thumbNail']['error'] element to determine if an actual file was successfully uploaded, because the ['name'] element will be set to a value for several of the possible upload errors. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2011 Share Posted February 14, 2011 Your code needs to test the $_FILES['thumbNail']['error'] element to determine if an actual file was successfully uploaded, because the ['name'] element will be set to a value for several of the possible upload errors. Great answer Quote Link to comment Share on other sites More sharing options...
noclist Posted February 14, 2011 Author Share Posted February 14, 2011 How do I go about testing for that error? I tried this with no luck: if ($_FILES['thumbNail']['error'] == 0) { $thumbNail = $_FILES['thumbNail']['name']; } else { $thumbNail="movieThumbs/noimage.gif"; Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 14, 2011 Share Posted February 14, 2011 http://www.php.net/manual/en/features.file-upload.errors.php Quote Link to comment Share on other sites More sharing options...
noclist Posted February 14, 2011 Author Share Posted February 14, 2011 Using this method... if($_FILES['userfile']['error']==0) { // process } else { // handle the error } if($_FILES['thumbNail']['error']==0) { $thumbNail = $_FILES['thumbNail']['name']; } else { $thumbNail="movieThumbs/noimage.gif"; } Still having the same issue. I must be doing something fundamentally wrong. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 14, 2011 Share Posted February 14, 2011 How about displaying all your post/files data to see what you are getting - echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; print_r($_FILES); echo "</pre>"; Quote Link to comment Share on other sites More sharing options...
noclist Posted February 14, 2011 Author Share Posted February 14, 2011 Here is what echoes for thumbNail: When I choose a thumbnail image to upload: [thumbNail] => Array ( [name] => x.gif [type] => image/gif [tmp_name] => /var/tmp/phpXylX84 [error] => 0 => 6577 ) When I leave the thumbnail field blank: [thumbNail] => Array ( [name] => [type] => [tmp_name] => [error] => 4 => 0 ) error 4 being no file uploaded, which is what I should see. I feel like I'm doing everything right except perhaps else { $thumbNail="movieThumbs/noimage.gif"; } Is that the correct way to assign the default image to that variable given my error 4? Quote Link to comment Share on other sites More sharing options...
noclist Posted February 14, 2011 Author Share Posted February 14, 2011 Update: Finally figured it out. Had to remove the 'movieThumbs/" when assigning the default image. Thanks to everyone who helped, mark it solved. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 14, 2011 Share Posted February 14, 2011 You can mark it solved. It's the green button to lower left that says 'Mark Solved' . . . Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.