Uploading Multiple Images with rename

Discussion in 'PHP' started by falconlink, Feb 10, 2011.

  1. #1
    Hi,

    I coded a form which works perfectly to upload a single image to remote server and can randomly rename the image. The information also be stored into database. But while I try to upload multiple images at a time, it occurs an error and I can't upload anything, even no information inserts into database.

    Database:
    Table - snapshots
    Fields - snap_id, cat_id, snapshots

    uploader.php
    <?php
    function getExtension($str)
    {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
    }

    if(isset($_POST['Submit']))
    {
    $catname=$_POST['selectCategory'];

    $image=$_FILES['image']['name'];
    $random=rand(000000,999999);
    if ($image)
    {
    //get the original name of the file from the clients machine
    $filename = stripslashes($_FILES['image']['name']);
    //get the extension of the file in a lower case format
    $extension = getExtension($filename);
    $extension = strtolower($extension);

    //we will give an unique name, for example the time in unix time format
    $image_name=$random.'.'.$extension;
    //the new name will be containing the full path where will be stored
    $newname="images/".$image_name;
    $copied = copy($_FILES['image']['tmp_name'], $newname);

    }
    $sql ="INSERT INTO snapshots(snap_id, cat_id, snapshots) VALUES('', '$catname', '$image_name')";
    if(mysql_query($sql))
    {
    $msg = "<div class='message'>Images successfully uploaded.</div>";
    }
    else
    {
    $msg = "<div class='message'>&nbsp;Failed to upload the images.</div>";
    }
    }
    ?>


    <form name="frmPic" action="<?php echo $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">

    Select Vehicle Name : <br />
    <select name="selectCategory">
    <option value="" selected>< Select One ></option>
    <option value="Flowers">Flowers</option>
    <option value="Fruits">Fruits</option>
    </select>

    Snapshot:<br />
    <input type="file" name="image[]" size="43" /><br />
    <input type="file" name="image[]" size="43" /><br />
    <input type="file" name="image[]" size="43" /><br />
    <input type="file" name="image[]" size="43" /><br />
    <input type="file" name="image[]" size="43" />

    <input type="submit" name="Submit" value="Upload Photo" />
    </form>
    ======================================================

    I hope, I could make you clear the problem. Can anybody help me to solve it and I just want to upload multiple images with rename at a time and stored into database.

    Thanks in advance.
     
    falconlink, Feb 10, 2011 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    What is the error? where does the error start

    start with error_reporting(E_ALL) at the top of your script!
     
    EricBruggema, Feb 10, 2011 IP
  3. falconlink

    falconlink Active Member

    Messages:
    285
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    78
    #3
    I tried to insert 5 images, but only 1 inserted into database without image extension like as (000000.), surprised no extension. but no image uploads into images directory.

    Error says,

    1. Warning: stripslashes() expects parameter 1 to be string, array given in D:\xampp\htdocs\dolphin\uploader.php on line 19
    I mean, errors in $filename = stripslashes($_FILES['image']['name']);

    2. Warning: copy() expects parameter 1 to be string, array given in D:\xampp\htdocs\dolphin\uploader.php on line 28
    I mean, errors in $copied = copy($_FILES['image']['tmp_name'], $newname);
     
    falconlink, Feb 11, 2011 IP
  4. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #4
    you have to do a loop like for 5 images
    for($i =0; $i<=5;$i++)
    {
    $image=$_FILES['image']['name'][$i];
    $image_name=$random.'.'.$extension;
    //the new name will be containing the full path where will be stored
    $newname="images/".$image_name;
    $copied = copy($_FILES['image']['tmp_name'][$i], $newname);
    //add your database here
    }
     
    crivion, Feb 11, 2011 IP
  5. falconlink

    falconlink Active Member

    Messages:
    285
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    78
    #5

    I do set a loop as your instruction and 5 images upload into directory successfully. But only 1 record saves into database. I wrote the code here-

    uploader.php
    <?php
    function getExtension($str)
    {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
    }

    if(isset($_POST['Submit']))
    {
    $catname=$_POST['selectCategory'];
    for($i =0; $i<=5;$i++)
    {

    $image=$_FILES['image']['name'][$i];
    $random=rand(000000,999999);
    if ($image)
    {
    //get the original name of the file from the clients machine
    $filename = stripslashes($_FILES['image']['name'][$i]);
    //get the extension of the file in a lower case format
    $extension = getExtension($filename);
    $extension = strtolower($extension);

    //we will give an unique name, for example the time in unix time format
    $image_name=$random.'.'.$extension;
    //the new name will be containing the full path where will be stored
    $newname="images/".$image_name;
    $copied = copy($_FILES['image']['tmp_name'][$i], $newname);
    }
    }
    $sql ="INSERT INTO snapshots(snap_id, cat_id, snapshots) VALUES('', '$catname', '$image_name')";
    if(mysql_query($sql))
    {
    $msg = "<div class='message'>Images successfully uploaded.</div>";
    }
    else
    {
    $msg = "<div class='message'>&nbsp;Failed to upload the images.</div>";
    }
    }
    ?>
     
    falconlink, Feb 11, 2011 IP
  6. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #6
    try this

    
    <?php
    function getExtension($str)
    {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
    }
    
    if(isset($_POST['Submit']))
    {
    $catname=$_POST['selectCategory'];
    for($i =0; $i<=5;$i++)
    {
    
    $image=$_FILES['image']['name'][$i];
    $random=rand(000000,999999);
    if ($image)
    {
    //get the original name of the file from the clients machine
    $filename = stripslashes($_FILES['image']['name'][$i]);
    //get the extension of the file in a lower case format
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    
    //we will give an unique name, for example the time in unix time format
    $image_name=$random.'.'.$extension;
    //the new name will be containing the full path where will be stored
    $newname="images/".$image_name;
    $copied = copy($_FILES['image']['tmp_name'][$i], $newname);
    $sql ="INSERT INTO snapshots(snap_id, cat_id, snapshots) VALUES('', '$catname', '$image_name')";
    if(mysql_query($sql))
    {
    $msg = "<div class='message'>Images successfully uploaded.</div>";
    }
    else
    {
    $msg = "<div class='message'>&nbsp;Failed to upload the images.</div>";
    }
    }
    }
    
    }
    ?>
    
    PHP:
     
    Last edited: Feb 11, 2011
    crivion, Feb 11, 2011 IP
  7. falconlink

    falconlink Active Member

    Messages:
    285
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    78
    #7
    Perfect, Thanks a lot crivion.
     
    falconlink, Feb 11, 2011 IP