SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    checking if files were even uploaded...

    I have a form which allows up to 6 file uploads...
    HTML Code:
          <table style="width:90%; margin:0 auto" class="form">
          <tr>
    <td><label for='file1'>Image 1:</label></td><td><input type='file' name='file[]'></a></td>
    <td><label for='file2'>Image 2:</label></td><td><input type='file' name='file[]'></a></td>
    </tr><tr>
    <td><label for='file3'>Image 3:</label></td><td><input type='file' name='file[]'></a></td>
    <td><label for='file4'>Image 4:</label></td><td><input type='file' name='file[]'></a></td>
    </tr><tr>
    <td><label for='file5'>Image 5:</label></td><td><input type='file' name='file[]'></a></td>
    <td><label for='file6'>Image 6:</label></td><td><input type='file' name='file[]'></a></td>
          </table>
    How do I check even if a file has baan selected, heres the code which runs on the form if a file has been selected to uipload
    PHP Code:
    foreach ($_FILES["file"]["name"] as $index => $file_name) {

    ...
    ...

    How do I run that code only if a file has been selected?
    I'm thinking I have to run something like this...
    PHP Code:
    if ($_FILES['file']['error'] != UPLOAD_ERR_NO_FILE) {
    ...
    ...

    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,034
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Dug this code up from an old thread:

    PHP Code:
    function get_files($_FILES) {
        
    $files=$_FILES['image'];
        
        
    $file_count=count($files);
        
    $count=0;
        
        while ( 
    $count <> $file_count-) {
            
    $sorted_files[$count]['name']=$files['name'][$count];
            
    $sorted_files[$count]['type']=$files['type'][$count];
            
    $sorted_files[$count]['tmp_name']=$files['tmp_name'][$count];
            
    $sorted_files[$count]['error']=$files['error'][$count];
            
    $sorted_files[$count]['size']=$files['size'][$count];
            if (!
    is_uploaded_file($sorted_files[$count]['tmp_name'])) {
                unset(
    $sorted_files[$count]);
            }
            
    $count++;
        }
        return 
    $sorted_files;

    That should take the $_FILES multidimensional array and rearrange it so that each sub-array of the $sorted_files array will relate to a single file and it'll deal with culling out any cases where no file has been uploaded.

    Just change the "image" in

    PHP Code:
    $files=$_FILES['image']; 
    to suit the form
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    So, when I run that function, I will get an array back of the uploaded files?
    I'm guessing that would go something like
    PHP Code:
    get_files();
    foreach (
    $sorted_files["name"] as $index => $file_name) {

    ...
    ...

    thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,034
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($sorted_files as $file) {
    // do something with the file here

    Each iteration of the loop you'll have a single file in $file
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  5. #5
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,648
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    the uploads dont seem to work, but I dont get an error..
    Heres my php code..
    PHP Code:
    function get_files($_FILES) {
        
    $files $_FILES['file'];
        
        
    $file_count=count($files);
        
    $count=0;
        
        while ( 
    $count <> $file_count-) {
            
    $sorted_files[$count]['name']=$files['name'][$count];
            
    $sorted_files[$count]['type']=$files['type'][$count];
            
    $sorted_files[$count]['tmp_name']=$files['tmp_name'][$count];
            
    $sorted_files[$count]['error']=$files['error'][$count];
            
    $sorted_files[$count]['size']=$files['size'][$count];
            if (!
    is_uploaded_file($sorted_files[$count]['tmp_name'])) {
                unset(
    $sorted_files[$count]);
            }
            
    $count++;
        }
        return 
    $sorted_files;

    get_files();

    foreach (
    $sorted_files as $file) {  

      if (
    $file == '') {
        continue;
      }

      
    $Type $file["type"];
      
    $Name $file["name"];
      
    $tmp_name $file["tmp_name"];
      
    $Error $file["error"];
      
    $Size $file["size"];
      if(
    $Error 0){
          echo 
    'An error ocurred when uploading.';
      }
      if((
    $Type != 'image/png') && ($Type != 'image/gif') && ($Type != 'image/jpeg') && ($Type != 'image/bmp'))
      {
          echo 
    'Unsupported filetype uploaded.';
      }
      if(
    $Size 102400){
          echo 
    'File uploaded exceeds maximum upload size.';
      }
      
    $now time(); 
      while(
    file_exists($uploadFilename $now.'-'.$Name)) 
      { 
          
    $now++; 
      } 
      if(!
    move_uploaded_file($tmp_name'uploads/'.$uploadFilename)){
          echo 
    'Error uploading file - check destination is writeable.';
      } 

    Thanks, im really trying to understand this, but I sort of get lost on the function (think I need a parameter, but dont know what to use on the methods call)
    "Oh, and Jenkins--apparently your mother died this morning."

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,034
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Add the following to the start of the script, the next line, after the opening <?php does PHP then report any errors?

    PHP Code:
    error_reporting(E_ALL E_STRICT E_ERROR E_WARNING E_PARSE E_NOTICE E_CORE_ERROR E_CORE_WARNING E_COMPILE_ERROR E_COMPILE_WARNING E_USER_ERROR E_USER_WARNING E_USER_NOTICE E_RECOVERABLE_ERROR);
    ini_set('display_errors'1); 
    The top two lines are supposed to be one line, it's gotten word-wrapped by the forum's text editor
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


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
  •