943,762 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 4080
  • PHP RSS
Jan 18th, 2008
0

Conditional if statement for <input type="file">

Expand Post »
I have a form that has an option to upload an image along with some text and a title. What I want to do is check if the input for uploading the image is empty. If it is then the page processes the code just to upload the text portion to the database. If it isn't empty then it processes the code that uploads the image as well as the text.

I used
php Syntax (Toggle Plain Text)
  1. if (!$_POST['image']) {
which at first I thought did the trick but when I tried uploading an image it didn't work. I know for a fact that the code for uploading an image works as I've used it on another page to test it. So I'm left to believe that the conditional statement is the source of the problem.

Is there another way of checking if someone has browsed and chosen an image file for uploading?
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Jan 18th, 2008
1

Re: Conditional if statement for <input type="file">

php Syntax (Toggle Plain Text)
  1. <?php
  2. if(isset($_POST['submit'])) {
  3. if($_FILES['fileupload']['name']==""){
  4. echo "Empty"; //no file was uploaded
  5. }
  6. //continue with rest of the operation..
  7. }
  8. ?>
  9. <html>
  10. <body>
  11. <form name="upload" method="post" action="upload.php" enctype="multipart/form-data">
  12. <table>
  13. <tr><td>
  14. <input type="file" name="fileupload">
  15. </td></tr>
  16. <tr><td>
  17. <input type="submit" name="submit">
  18. </td></tr>
  19. </table>
  20. </form>
  21. </body>
  22. </html>
This will do..
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jan 18th, 2008
0

Re: Conditional if statement for <input type="file">

I could kick myself right now. Thanks once again nav33n. May I one day be as incredibly smart as you are
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Jan 18th, 2008
0

Re: Conditional if statement for <input type="file">

You are welcome!
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jan 18th, 2008
2

Re: Conditional if statement for <input type="file">

Just for those that would like to know what I wanted the conditional statement for, here's an example:

php Syntax (Toggle Plain Text)
  1. if ($_REQUEST['submit']) {
  2.  
  3. //checks if there is no image selected for uploading
  4. if ($_FILES['image']['name'] == '' ) {
  5.  
  6. //the code for inserting the text into the database
  7.  
  8. } else {
  9.  
  10. //the code for uploading the image and the text.
  11.  
  12. }
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Help! Parse error: parse error, unexpected T_STRING
Next Thread in PHP Forum Timeline: object oriented php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC