SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_FILES check if empty

    When I use the following to check if files exist, the form still submits as successfull even when no files are added?

    PHP Code:
    if(!$_FILES['photoname']) { 
         
    $error_msg 'You must upload your portfolio photos';


    Field formated like so:

    PHP Code:
    while($counter <= $number_of_fields){
         echo 
    '<input name="photoname[]" type="file" id="upload" />';
    $counter++;

    Thanks

  2. #2
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ( $_FILES['whatever']['error'] > UPLOAD_ERR_OK )
      
    // we have an error! 
    The rest is an exercise for you.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I will look into it..

    But shouldnt if(!$_FILES['photoname']) work.. as its just like checking on post or get for empty fields?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,094
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by _matrix_ View Post
    But shouldnt if(!$_FILES['photoname']) work.. as its just like checking on post or get for empty fields?
    Nope. It's the same as a text field; when it is submitted but there was no text filled in $_POST['myfield'] is still set, but it's set to an empty string.
    $_FILES['photoname'] is also set, but there is no file in there. Same thing.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if($_FILES['photoname']['error'] > 0) { 
          
    $error_msg 'You must upload a photo';

    From what I read, the above should work?

    Altho it appears to stop the script continuing even when atleast one file is uploaded..

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,094
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Maybe the file you're uploading is too big? Default max size for PHP uploads is 2MB if I remember correctly (plus there is a limit on the web server as well, if that's lower than PHP's limit that's the limit you need to honour).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    File sizes are small.. kb's.. I tried filling all 5 fields with files.. but $error_msg alway populates with error message

  8. #8
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Maybe you should read the error messages that is why they are there.
    http://www.php.net/manual/en/feature...oad.errors.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are no errors.. but I guess the problem is because its an array of files being sent.. not a single file..

    Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 ) 0

  10. #10
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,094
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    In that case you could check

    PHP Code:
    if (array_sum($_FILES['photoname']['error']) > 0) {
        
    // There was an error 

    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •