www.webdeveloper.com
Results 1 to 7 of 7

Thread: getimagesize for multipe image upload

  1. #1
    Join Date
    Mar 2013
    Posts
    18

    getimagesize for multipe image upload

    Hi I wanted to validate if the uploaded file is images by using the below script. However, I get a warning message saying that it required string and an array is given. How do I modify the script to validate multiple upload (I show 4 upload file with [] in the form). thanks.

    PHP Code:
    $imageinfo getimagesize($_FILES['userfile']['tmp_name']);
        if(
    $imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') {
        echo 
    "Sorry, we only accept GIF and JPEG images\n";
        exit;


  2. #2
    Join Date
    May 2011
    Posts
    101
    The arrays:

    $_FILES['userfile'],
    $_FILES['userfile']['name'],

    and

    $_FILES['userfile']['size']

    are set up when you upload multiple images.

    Cycle through these, and not through:

    ($_FILES['userfile']['tmp_name'])

    which is not an array.

    I scratched my head for a long time, doing a site for
    an estate agent about three years ago, on that one.

    C
    Last edited by chriseccles; 04-01-2013 at 04:06 AM.
    Sometimes, when you re-invent the wheel, you end up with a better wheel.
    ---------------------------------------------
    One-on-One Scripting Tuition/Mentoring

  3. #3
    Join Date
    Mar 2013
    Posts
    18
    Can you please provide a sample code? I am new to php and do not know how to use this. thx.

  4. #4
    Join Date
    May 2011
    Posts
    101
    Something along these lines, I guess:

    I always use a "reference " field in the form so I can
    tag it on the beginning of the filename, but you can leave that
    out if you wish. Make SURE permissions on your receptor directory
    on the remote server are correctly set, otherwise the pics won't
    upload. the loader below at least tells you there's a problem.
    Some don't.

    Code:
    foreach($_FILES["fileup"]["error"] as $key => $error) {                                   // BIG LOOP
    
        $ref = $reference;
        $newName = "";
        $uploadpath = 'upload/';                                                              // directory to store the uploaded files
    
        if($error == UPLOAD_ERR_OK) {                                                         // MINOR LOOP
            $newName = $ref . basename( $_FILES['fileup']['name'][$key]);                     // append the reference
            echo "File Temporary Location: " . $_FILES['fileup']['tmp_name'][$key] . "<br/><br/>";
            $uploadpath = $uploadpath . $newName;                                             // gets the file name
            $sepext = explode('.', strtolower($_FILES['fileup']['name'][$key]));
            $type = end($sepext);                                                             // gets extension
            list($width, $height) = getimagesize($_FILES['fileup']['tmp_name'][$key]);
            $err = '';                                                                        // to store the errors
                if(!in_array($type, $allowtype)) {                                            // Check type/size/width/height
                  $err .= 'The file: <b>'. $_FILES['fileup']['name'][$key] . '</b> not the allowed extension type.';
                }
                    if($_FILES['fileup']['size'][$key] > $max_size*1000) {
                      $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
                    }
                      if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) {
                        $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;
                      }
                                                                                              // If no errors, upload the image, else, output the errors
                    if($err == '') {
                      if(move_uploaded_file($_FILES['fileup']['tmp_name'][$key], $uploadpath)) { 
                      echo 'File: <b>'. basename( $_FILES['fileup']['name'][$key]). '</b> successfully uploaded:';
                      echo '<br/>File type: <b>'. $_FILES['fileup']['type'][$key] .'</b>';
                      echo '<br />Size: <b>'. number_format($_FILES['fileup']['size'][$key]/1024, 3, '.', '') .'</b> KB';
                        if(isset($width) && isset($height)) {
                          echo '<br/>Image Width x Height: '. $width. ' x '. $height;
                        }
                          echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b>';
                      }
                      else echo '<b>Unable to upload the file.</b>';
                    }
                    else echo $err;
                    echo "<br/><br/> File Reference: " . $ref;
        }
      
    }
    Sometimes, when you re-invent the wheel, you end up with a better wheel.
    ---------------------------------------------
    One-on-One Scripting Tuition/Mentoring

  5. #5
    Join Date
    Mar 2013
    Posts
    18
    Hi chriseccles,

    I tried your script and it gives me error message of not the allowed extension type doesn't matter what file type I attached to it. I have made the $allowtype to an array $allowtype = array("image/jpeg", "image/png", "image/jpg") Could you please help with this problem? Also, I am looking to have the image attached to an email and send it out with phpmailer. Can you change the script to do that instead of the upload type as what you have now? Thx.

  6. #6
    Join Date
    May 2011
    Posts
    101
    Hi carygh

    The array, $allowtype, should be set up as ("gif", "jpg", "tiff", "png") etc etc

    NOT "image/jpg" etc

    Then it works flawlessly.

    Full script:

    PHP Code:
    <?php
    /*    MULTI-FILE UPLOADER

          Chris Eccles 2011
          
          
    */

    error_reporting(E_ALL);

    $tempDirectory sys_get_temp_dir();
    echo 
    "The filesystem tmp_dir is: " $tempDirectory "<br/><br/>";

    $max_size 2000;                                                                         // maximum file size, in KB
    $alwidth 2000;                                                                          // maximum allowed width, in pixels
    $alheight 1000;                                                                         // maximum allowed height, in pixels

    $allowtype = array('pdf''bmp''gif''jpg''jpeg''png');                                   // allowed extensions

    $reference $_POST["reference"];                                                         // retrieve file reference on form

    foreach($_FILES["fileup"]["error"] as $key => $error) {                                   // BIG LOOP

        
    $ref $reference;
        
    $newName "";
        
    $uploadpath 'upload/';                                                              // directory to store the uploaded files

        
    if($error == UPLOAD_ERR_OK) {                                                         // MINOR LOOP

            
    $sepext explode('.'strtolower($_FILES['fileup']['name'][$key]));
            
    $type end($sepext);                                                             // gets extension

    // *********************************************************  NAMING THE UPLOADED FILE  ******************************************************************   
         
                                                                                              // append the reference:
    //        $newName = $ref . basename( $_FILES['fileup']['name'][$key]);
                                                                                              // OR replace name with reference:
            
    $newName $ref "." $type;

    // *******************************************************************************************************************************************************        

            
    echo "File Temporary Location: " $_FILES['fileup']['tmp_name'][$key] . "<br/><br/>";
            
    $uploadpath $uploadpath $newName;                                             // gets the file name
            
    list($width$height) = getimagesize($_FILES['fileup']['tmp_name'][$key]);
            
    $err '';                                                                        // to store the errors
                
    if(!in_array($type$allowtype)) {                                            // Check type/size/width/height
                  
    $err .= 'The file: <b>'$_FILES['fileup']['name'][$key] . '</b> not the allowed extension type.';
                }
                    if(
    $_FILES['fileup']['size'][$key] > $max_size*1000) {
                      
    $err .= '<br/>Maximum file size must be: '$max_size' KB.';
                    }
                      if(isset(
    $width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) {
                        
    $err .= '<br/>The maximum Width x Height must be: '$alwidth' x '$alheight;
                      }
                                                                                              
    // If no errors, upload the image, else, output the errors
                    
    if($err == '') {
                      if(
    move_uploaded_file($_FILES['fileup']['tmp_name'][$key], $uploadpath)) { 
                      echo 
    'File: <b>'basename$_FILES['fileup']['name'][$key]). '</b> successfully uploaded:';
                      echo 
    '<br/>File type: <b>'$_FILES['fileup']['type'][$key] .'</b>';
                      echo 
    '<br />Size: <b>'number_format($_FILES['fileup']['size'][$key]/10243'.''') .'</b> KB';
                        if(isset(
    $width) && isset($height)) {
                          echo 
    '<br/>Image Width x Height: '$width' x '$height;
                        }

                          echo 
    '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b>';

    // *************************************************  ENSURE UPLOADED FILE CAN BE READ BY ALL  ***********************************************************   

                          
    chmod("$uploadpath"0644);
                          
    // *******************************************************************************************************************************************************        

                      
    }
                      else echo 
    '<b>Unable to upload the file.</b>';
                    }
                    else echo 
    $err;
                    echo 
    "<br/><br/> File Reference: " $ref;
        }
      
    }

    ?> 
    <div style="margin:1em auto; width:333px; text-align:center;">
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
      Reference: <input type="text" name="reference"><br>
      Upload File: <input type="file" name="fileup[0]" /><br/>
      Upload File: <input type="file" name="fileup[1]" /><br/>
      Upload File: <input type="file" name="fileup[2]" /><br/>
      Upload File: <input type="file" name="fileup[3]" /><br/>

      <input type="submit" name='submit' value="Upload" /> 
     </form>
    </div>
    C
    Last edited by chriseccles; 04-12-2013 at 02:07 AM. Reason: Added full script
    Sometimes, when you re-invent the wheel, you end up with a better wheel.
    ---------------------------------------------
    One-on-One Scripting Tuition/Mentoring

  7. #7
    Join Date
    May 2011
    Posts
    101
    Confirm that this now works for you, carygh !
    -
    I don't like mysteries

    C
    Sometimes, when you re-invent the wheel, you end up with a better wheel.
    ---------------------------------------------
    One-on-One Scripting Tuition/Mentoring

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles