SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    multiple upload file type validation

    hi all

    i m using below multiple upload code but its not allowing me to upload images

    if i remove file type check validation code then everything works fine

    PHP Code:
    <?php
    $conn 
    mysql_connect("localhost","root",'') or die(mysql_error());
    mysql_select_db("test",$conn);
    if(isset(
    $_FILES['files'])){

        foreach(
    $_FILES['files']['tmp_name'] as $key => $tmp_name ){
            
    $file_name $key.$_FILES['files']['name'][$key];
            
    $file_size =$_FILES['files']['size'][$key];
            
    $file_tmp =$_FILES['files']['tmp_name'][$key];
            
    $file_type=$_FILES['files']['type'][$key];    

            if(
    $file_size 2097152){
                echo 
    $file_name .' exceeds limit of 2 MB <br>';
                
    $errors true;
            }

            if ( (
    $file_type != "image/gif") || ($file_type != "image/jpeg") || ($file_type != "image/png") || ($file_type != "image/pjpeg") )
            {
            echo 
    $file_name .' extension is not allowed <br>';
            
    $errors true;
            }
            
            
    //echo $query."<br>";
            
    $desired_dir="multiple_images_folder";
            if(
    $errors == false){
                if(
    is_dir($desired_dir)==false){
                    
    mkdir("$desired_dir"0700);// Create directory if it does not exist
                
    }
                if(
    is_dir("$desired_dir/".$file_name)==false){
                    
    move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
                }else{                                    
    // rename the file if another one exist
                    
    $new_dir="$desired_dir/".$file_name.time();
                     
    rename($file_tmp,$new_dir) ;                
                }
            
    $query="INSERT into upload_data (file_name,file_size,file_type) VALUES('$file_name','$file_size','$file_type'); ";    
             
    mysql_query($query);            
            }else{
                    echo 
    "there are some errors";
            }
        }
        if(
    $errors == false){
            echo 
    "Success";
        }
    }
    ?>

    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="files[]" /><br />
        <input type="file" name="files[]" /><br />
        <input type="file" name="files[]" /><br />
        <input type="file" name="files[]" /><br />
        <input type="submit" name="submit" value="upload multiple"/>
    </form>
    vineet

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    if ( ($file_type != "image/gif") || ($file_type != "image/jpeg") || ($file_type != "image/png") || ($file_type != "image/pjpeg") )

    You mean && instead of ||. A file cannot be all of those types at the same time.

    in order for !A | !B | !C | !D to be evaulated as false (the desired outcome), all four conditions must be True. Since $file_type cannot be more than one of those four strings at the same time, at least 3 of the conditions are false.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi starlion

    i replaced || with &&

    If i upload 4 images in 4 input boxes then all is fine.

    But
    If in 1st and 2nd input box i upload PDF files and in 3rd and 4th input box i upload IMAGE files then nothing gets uploaded.
    Can it be possible to upload the 2 image files and leave the rest 2 PDF files

    PHP Code:
    <?php
    $conn 
    mysql_connect("localhost","root",'') or die(mysql_error());
    mysql_select_db("test",$conn);
    if(isset(
    $_FILES['files'])){

        foreach(
    $_FILES['files']['tmp_name'] as $key => $tmp_name ){
            
    $file_name $key.$_FILES['files']['name'][$key];
            
    $file_size =$_FILES['files']['size'][$key];
            
    $file_tmp =$_FILES['files']['tmp_name'][$key];
            
    $file_type=$_FILES['files']['type'][$key];    

            if(
    $file_size 2097152){
                echo 
    $file_name .' exceeds limit of 2 MB <br>';
                
    $errors true;
            }

            if ( (
    $file_type != "image/gif") && ($file_type != "image/jpeg") && ($file_type != "image/png") && ($file_type != "image/pjpeg") )
            {
            echo 
    $file_name .'extension not allowed';
            
    $errors true;
            }
            
    //echo $query."<br>";
            
    $desired_dir="multiple_images_folder";
            if(
    $errors == false){
                if(
    is_dir($desired_dir)==false){
                    
    mkdir("$desired_dir"0700);// Create directory if it does not exist
                
    }
                if(
    is_dir("$desired_dir/".$file_name)==false){
                    
    move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
                }else{                                    
    // rename the file if another one exist
                    
    $new_dir="$desired_dir/".$file_name.time();
                     
    rename($file_tmp,$new_dir) ;                
                }
            
    $query="INSERT into upload_data (file_name,file_size,file_type) VALUES('$file_name','$file_size','$file_type'); ";    
             
    mysql_query($query);            
            }
            
            
            else{
                    echo 
    $file_name ." File type Extension not allowed <br>";
            }
        }
        if(
    $errors == false){
            echo 
    "Success";
        }
    }
    ?>


    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="files[]" /><br />
        <input type="file" name="files[]" /><br />
        <input type="file" name="files[]" /><br />
        <input type="file" name="files[]" /><br />
        <input type="submit" name="submit" value="upload multiple"/>
    </form>
    vineet

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    You need to reset $errors to false on each loop pass, or it will stop processing them as soon as it encounters one error.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi starlion

    at present the IF statement is already inside the foreach loop. so i think its checking one by one.

    So should i insert another foreach loop or i should try some other method.

    what do you suggest

    vineet

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    I know the IF is inside the loop, but think about this:
    Start at the top of your code
    File 1: Type PDF
    $errors = true; (previously undefined)
    If($errors = false) skipped; TRUE instead.
    End Loop
    File 2: Type JPEG
    $errors does not get changed
    If($errors = false) skipped; $errors is still TRUE.

    I suggest sticking $errors = false; right as the foreach begins.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •