www.webdeveloper.com
Results 1 to 3 of 3

Thread: validating a multi-image upload

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    validating a multi-image upload

    Hi All,
    Im building a CMS where 3 images need to be uploaded. I'm trying to validate by checking for 2 things (at least for now). One, that it's not empty and
    Two, the image must be a .gif

    3 images in an upload means a 2 dimensional array. I've accessed via 2 foreach loops but am trying to figure out the syntax isolate and check the individaul key => value pairs.

    echoing the 2 foreach loops produces this...

    Array
    (
    [add_acchdr_off] => Array (
    [name] => accodion_header_off.gif
    [type] => image/gif
    [tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpGR9rW9
    [error] => 0
    [size] => 420681
    )
    [add_acchdr_on] => Array (
    [name] => accodion_header_on.gif
    [type] => image/png
    [tmp_name] => /Applications/XAMPP/xamppfiles/temp/php1B91mn
    [error] => 0
    [size] => 200434
    )
    [add_main_pic] => Array (
    [name] =>
    [type] =>
    [tmp_name] =>
    [error] => 4
    [size] => 0
    )
    )

    i want to access the [error] and test if it's 4 (means it's empty), and also the [type] to see if it's a .gif or not. how can i do that?

    any help would be great, thanks!

    my code so far is this,
    $imgArr = array($add_acchdr_off, $add_acchdr_on, $add_main_pic);
    foreach ($imgArr as $Img) {
    foreach($Img as $key => $val) {
    echo $key . ' ' . $val;
    //put some val code here
    }
    }
    Last edited by toptomato; 06-13-2012 at 08:44 AM.

  2. #2
    Join Date
    Jun 2012
    Posts
    6
    I know this doesn't answer your question, but do you have any links on how to go about uploading multiple images? I'm working on a 2 image uploader and I want it to upload each image in a separate locations. Help would be much appreciated!

  3. #3
    Join Date
    Mar 2010
    Posts
    281
    Almost!, in fact i've answered my own question re: validating. Next i have to script how to move from temp to designated folder. I will post later if you want, let me know

    First off, to understand the Super Global Files array, i would do this, it shows you everything

    PHP Code:
    if(isset($_POST['submit'])) {
    echo 
    '<pre>'.print_r($_FILEStrue).'</pre>';

    don't know if this is 100%, i've distilled from my project and cut out stuff not relevant to img uploads.

    PHP Code:
    function valImg($img, &$theErrArr) {
    if(
    $img['error'] == ){
        
    //echo 'Img is empty, please choose an image';
        
    array_push($theErrArr'Img is empty, please choose an image before submitting');
        }

    if(
    $img['type'] != 'image/gif') {
        
    //echo '<p>Img type must a gif</p>';
        
    array_push($theErrArr'Img type must a gif');
        }     
    }

    function 
    display_errors($errArr) {
    $count count($errArr);
    for(
    $i=0;$i<$count;$i++) {
        echo 
    $errArr[$i] . '<br>';
        }
    }


    if(isset(
    $_POST['submit'])) {

    //create image vars
    $header_off $_FILES['header_off'];
    $header_on $_FILES['header_on'];
    $main_pic $_FILES['main_pic'];

    //I've chose to create array manually, but you could do with Super Globals //$_FILES[]
    $imgArr = array($header_off$header_on$main_pic);

    //the error array
    $addErrArr = array();

    foreach(
    $imgArr as $indImg) {
        
    valImg($indImg);
    }

    if(!empty(
    $addErrArr) {
        
    display_errors($addErrArr);
    } else {
        
    //Run Query, update tables, copy images to dir, etc. etc.
    }



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