www.webdeveloper.com
Results 1 to 4 of 4

Thread: Multiple image upload en resize

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21

    Multiple image upload en resize

    Hi everybody!

    I'm getting crazy :P I want to make a script that uploads some image files and saves the image URL's to the database... But the problem is, it only uploads the last input field :S

    This is the code:

    PHP Code:
    <?php
    include('functions/connection.php');
    include(
    "functions/check_login.php");
    include(
    "functions/check_admin.php");
    //voor de foto upload


    //VOOR DE ADMIN CHECK
    $sql="SELECT * FROM users";
    $result=mysql_query($sql);
    $data=mysql_fetch_array($result);
    $gebruiker=$_SESSION['username'];
    $aantal $_GET['x'];
    ?>
    <html>
    <head>


    </head>
    <body>
    <h3>portfolio item toevoegen</h3>

    <fieldset class="dashboard">
    <legend class="dashboard">Beheer</legend>
    <?php
    if (isset($_REQUEST['submit'])) { 
        
    $name $_POST["name"];
        
    $description $_POST["description"];
        
    $video $_POST["video"];
        
        for(
    $i=0$i count($aantal); $i++) {
        
            
    //foto uploaden
        
                
    $path_thumbs "upload/thumbs";
                
    $path_big "upload/images";
                
                
    //the new width of the resized image.
                    
    $img_thumb_width 150// in pixel
                //Do you want to limit the extensions of files uploaded (yes/no)
                    
    $extlimit "no"
                
    //allowed Extensions
                    
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
                
    $file_type $_FILES['imgfile']['type'][$i];
                
    $file_name $_FILES['imgfile']['name'][$i];
                
    $file_size $_FILES['imgfile']['size'][$i];
                
    $file_tmp $_FILES['imgfile']['tmp_name'][$i];
        
               
    //check file extension
               
    $ext strrchr($file_name,'.');
               
    $ext strtolower($ext);
               if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) {
                  echo 
    "Verkeerde extensie.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                  exit();
               }
               
    //get the file extension.
               
               
    $file_ext end(explode("."$file_name));
        
               
    //create a random file name
               
    $rand_name md5(time());
               
    $rand_namerand(0,999999999);
               
    //get the new width variable.
               
    $ThumbWidth $img_thumb_width;
        
               
    //keep image type
               
    if($file_size){
                  if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                       
    $new_img imagecreatefromjpeg($file_tmp);
                   }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){
                       
    $new_img imagecreatefrompng($file_tmp);
                   }elseif(
    $file_type == "image/gif"){
                       
    $new_img imagecreatefromgif($file_tmp);
                   }
                   
    //list width and height and keep height ratio.
                   
    list($width$height) = getimagesize($file_tmp);
                   
    $imgratio=$width/$height;
                   if (
    $imgratio>1){
                      
    $newwidth $ThumbWidth;
                      
    $newheight $ThumbWidth/$imgratio;
                   }else{
                         
    $newheight $ThumbWidth;
                         
    $newwidth $ThumbWidth*$imgratio;
                   }
                   
    //function for resize image.
                   
    if (function_exists(imagecreatetruecolor)){
                   
    $resized_img imagecreatetruecolor($newwidth,$newheight);
                   }else{
                         die(
    "Error: Please make sure you have GD library ver 2+");
                   }
                   
    imagecopyresized($resized_img$new_img0000$newwidth$newheight$width$height);
                   
    //save image
                   
    ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
                   
    ImageDestroy ($resized_img);
                   
    ImageDestroy ($new_img);
                   
    move_uploaded_file ($file_tmp"$path_big/$rand_name.$file_ext");
                   
                }
        


        
    //foto's toevoegen
            
    $sql2 "INSERT INTO images (link,thumb,name) VALUES ('$path_big/$rand_name.$file_ext','$path_thumbs/$rand_name.$file_ext','$name')";
        }
            
    //algemene data invoegen
            
    $sql "INSERT INTO projecten (name,description,video) VALUES ('$name','$description','$video')";
            
            if( (
    $result mysql_query($sql)) && ($result2 mysql_query($sql2)) ) { 
                echo 
    '<p><img src="images/icons/accept.gif" alt"" /> Portfolio item succesvol aangemaakt!</p>'
                
    $page "index.php?page=portfolio";
                
    $sec "1";
                
    //header("Refresh: $sec; url=$page");
            
    } else { 
                echo 
    "ERROR: ".mysql_error(); 
            } 
    } else { 
    ?>

    <div id="container">

    <form action="" enctype="multipart/form-data" method="post" class="niceform" name="UD">

    <dl>
        <dt><label for="name">Titel:</label></dt>
        <dd><input type="text" name="name" id="name" size="32" maxlength="128" /></dd>
    </dl>
    <dl>
        <dt><label for="description">Beschrijving:</label></dt>
        <dd><textarea name="description" id="description" rows="8" cols="65"></textarea></dd>
    </dl>
    <?php for($i=0$i $aantal$i++) { ?>
        <dl>
            <dt><label for="imgfile">Kies foto:</label></dt>
            <dd><input name="imgfile[]" id="imgfile[]" type="file" /></dd>
        </dl>
        <?php ?>
    <dl>
        <dt><label for="video">(Embed) Video:</label></dt>
        <dd><textarea name="video" id="video" rows="8" cols="65"></textarea></dd>
    </dl>
            <dd><input type="submit" name="submit" id="submit" value="voeg toe" onClick="return validateForm()" /></dd>

    </form>
    </div>
    </fieldset>

    <?php
    }
    ?>
    </body>
    </html>
    Does anyone see's the problem :S I can't get it worked.....

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    PHP Code:
    for($i=0$i count($aantal); $i++) { 
    You use method POST for the upload form submit but $aantal is populated to whatever $_GET['x'] is set and if it's not being set on submit then it's defaulting to 0, and the loop iterates only once. This is based on the code as posted here in this topic. Debug it to ensure its set properly. Or set that to a static integer, or use foreach.

    And consider this instead, i.e. to default $anntal sensibly if $_GET['x'] is invalid or not set (your method is prone to injection and security issues):

    PHP Code:
    // Set to URL argument x or default 10
    $aantal=(isset($_GET['x']) && int(trim($_GET['x'])) <11) ? int(trim($_GET['x'])) : 10
    You get the idea, this is just a quick example off the top of my head.

    -jim
    Jim, Sr. Web Developer
    You know who you real friends are when you ask them to move your furniture or paint.

  3. #3
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21
    OK, i made a default $aantal by adding this:

    PHP Code:
    if(!isset($_GET['x'])){
        
    $aantal=2;
    }else{
        
    $aantal$_GET['x'];

    But i still have the problem that only the first field is submitted....
    Do I have to use the foreach function, and maybe somebody can explain it for me. I really don't understand that function :P

    (Crappy english again, i'm sorry :P )

  4. #4
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21
    OK, I did a lot of research and finally I made some progress
    When I submit the form now, it's uploads (and resizes) all the images i enter at the input fields.
    But it only inserts the first image url into the database...

    This is the code at the moment:

    PHP Code:
    <?php
    include('functions/connection.php');
    include(
    "functions/check_login.php");
    include(
    "functions/check_admin.php");

    //VOOR DE ADMIN CHECK
    $sql="SELECT * FROM xxx";
    $result=mysql_query($sql);
    $data=mysql_fetch_array($result);
    $gebruiker=$_SESSION['username'];
    if(!isset(
    $_GET['x'])){
        
    $aantal=2;
    }else{
        
    $aantal$_GET['x'];
    }
    ?>
    <html>
    <head>


    </head>
    <body>
    <h3>portfolio item toevoegen</h3>

    <fieldset class="dashboard">
    <legend class="dashboard">Beheer</legend>
    <?php
    if (isset($_REQUEST['submit'])) { 
        
    $name $_POST["name"];
        
    $description $_POST["description"];
        
    $video $_POST["video"];
        
        
    $fieldname 'imgfile';
        
    $active_keys = array();
        foreach(
    $_FILES[$fieldname]['name'] as $key => $filename) {
            if(!empty(
    $filename)) {
                
    $active_keys[] = $key;
            }
        }
            
            foreach(
    $active_keys as $key) {
        
            
    //foto uploaden
        
                
    $path_thumbs "upload/thumbs";
                
    $path_big "upload/images";
                
                
    //the new width of the resized image.
                    
    $img_thumb_width 150// in pixel
                //Do you want to limit the extensions of files uploaded (yes/no)
                    
    $extlimit "no"
                
    //allowed Extensions
                    
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
                
    $file_type $_FILES['imgfile']['type'][$key];
                
    $file_name $_FILES['imgfile']['name'][$key];
                
    $file_size $_FILES['imgfile']['size'][$key];
                
    $file_tmp $_FILES['imgfile']['tmp_name'][$key];
        
               
    //check file extension
               
    $ext strrchr($file_name,'.');
               
    $ext strtolower($ext);
               if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) {
                  echo 
    "Verkeerde extensie.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                  exit();
               }
               
    //get the file extension.
               
               
    $file_ext end(explode("."$file_name));
        
               
    //create a random file name
               
    $rand_name md5(time());
               
    $rand_namerand(0,999999999);
               
    //get the new width variable.
               
    $ThumbWidth $img_thumb_width;
        
               
    //keep image type
               
    if($file_size){
                  if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                       
    $new_img imagecreatefromjpeg($file_tmp);
                   }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){
                       
    $new_img imagecreatefrompng($file_tmp);
                   }elseif(
    $file_type == "image/gif"){
                       
    $new_img imagecreatefromgif($file_tmp);
                   }
                   
    //list width and height and keep height ratio.
                   
    list($width$height) = getimagesize($file_tmp);
                   
    $imgratio=$width/$height;
                   if (
    $imgratio>1){
                      
    $newwidth $ThumbWidth;
                      
    $newheight $ThumbWidth/$imgratio;
                   }else{
                         
    $newheight $ThumbWidth;
                         
    $newwidth $ThumbWidth*$imgratio;
                   }
                   
    //function for resize image.
                   
    if (function_exists(imagecreatetruecolor)){
                   
    $resized_img imagecreatetruecolor($newwidth,$newheight);
                   }else{
                         die(
    "Error: Please make sure you have GD library ver 2+");
                   }
                   
    imagecopyresized($resized_img$new_img0000$newwidth$newheight$width$height);
                   
    //save image
                   
    ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
                   
    ImageDestroy ($resized_img);
                   
    ImageDestroy ($new_img);
                   
    move_uploaded_file ($file_tmp"$path_big/$rand_name.$file_ext");
                   
                       
    //foto's toevoegen
                    
    foreach($active_keys as $key) { 
                        
    mysql_query("INSERT INTO images (link,thumb,name) VALUES ('$path_big/$rand_name.$file_ext','$path_thumbs/$rand_name.$file_ext','$name')");
                   }
                }
            }
            
    //algemene data invoegen
            
    $sql "INSERT INTO projecten (name,description,video) VALUES ('$name','$description','$video')";
            
            if( (
    $result mysql_query($sql)) ) { 
                echo 
    '<p><img src="images/icons/accept.gif" alt"" /> Portfolio item succesvol aangemaakt!</p>'
                
    $page "index.php?page=portfolio";
                
    $sec "1";
                
    //header("Refresh: $sec; url=$page");
            
    } else { 
                echo 
    "ERROR: ".mysql_error(); 
            } 
    } else { 
    ?>

    <div id="container">

    <form action="" enctype="multipart/form-data" method="post" class="niceform" name="UD">

    <dl>
        <dt><label for="name">Titel:</label></dt>
        <dd><input type="text" name="name" id="name" size="32" maxlength="128" /></dd>
    </dl>
    <dl>
        <dt><label for="description">Beschrijving:</label></dt>
        <dd><textarea name="description" id="description" rows="8" cols="65"></textarea></dd>
    </dl>
    <?php for($i=0$i $aantal$i++) { ?>
        <dl>
            <dt><label for="imgfile">Kies foto:</label></dt>
            <dd><input name="imgfile[]" id="imgfile[]" type="file" /></dd>
        </dl>
        <?php ?>
    <dl>
        <dt><label for="video">(Embed) Video:</label></dt>
        <dd><textarea name="video" id="video" rows="8" cols="65"></textarea></dd>
    </dl>
            <dd><input type="submit" name="submit" id="submit" value="voeg toe" onClick="return validateForm()" /></dd>

    </form>
    </div>
    </fieldset>

    <?php
    }
    ?>
    </body>
    </html>

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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