How to Upload Multiple Images by PHP coding at single time?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paul
    Junior Member
    • May 2004
    • 11

    How to Upload Multiple Images by PHP coding at single time?

    I need to be able to upload 10 images in total at the same time, to the same directory (created using mkdir)

    When the image is uploaded the name is changed to $vdesc_id.jpg which works great, but each image will need a differnt name,
  • kevin
    Love is life
    • Dec 2015
    • 83

    #2
    Hi

    You can use belwo code to upload multiple image at the single time...


    #######################
    <?php include("init.php");?>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>


    <?
    echo "$vdesc_id";

    if(!file_exists("../gfx/stock/$vdesc_id/"))
    {
    mkdir("../gfx/stock/$vdesc_id/", 0755);
    chmod("../gfx/stock/$vdesc_id/", 0777);
    }

    $form = TRUE;
    $upload_path = "../gfx/stock/$vdesc_id/";

    $fname = 1;

    for ($i=0;$i<10;$i++)
    {
    list($problem, $error) = upload_file ($upload_path, $vdesc_id.$fname.".jpg", $i);
    if (!$problem)
    {
    $fname++;
    }
    else
    echo $i.": ".$error."<br>";
    }

    echo "<br>";
    echo --$fname." file(s) uploaded";



    if ($form)
    {
    ?>
    <form enctype="multipart/form-data" action="<?php echo $SELF_PHP; ?>" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="300000">
    0: <input cl***="file" type="file" name="file[0]"><br>
    1: <input cl***="file" type="file" name="file[1]"><br>
    2: <input cl***="file" type="file" name="file[2]"><br>
    3: <input cl***="file" type="file" name="file[3]"><br>
    4: <input cl***="file" type="file" name="file[4]"><br>
    5: <input cl***="file" type="file" name="file[5]"><br>
    6: <input cl***="file" type="file" name="file[6]"><br>
    7: <input cl***="file" type="file" name="file[7]"><br>
    8: <input cl***="file" type="file" name="file[8]"><br>
    9: <input cl***="file" type="file" name="file[9]"><br>
    <input type="submit" value="Submit Images">
    </form>
    <?
    }


    function upload_file ($upload_path, $fname, $fileid)
    {
    $problem = TRUE;

    switch ($_FILES['file']['error'][$fileid])
    {
    case UPLOAD_ERR_FORM_SIZE:
    $error = "The uploaded file exceeds the 85kb";
    break;
    case UPLOAD_ERR_NO_FILE:
    $error = "No file was uploaded";
    break;
    default:
    $error = "";
    }


    //$fname = $_FILES['file']['name'][$fileid];
    $dest = $upload_path.$fname;
    $tmpfile = $_FILES['file']['tmp_name'][$fileid];

    if ($tmpfile)
    {
    $info = getimagesize($tmpfile);
    $ftype = $info['mime'];

    if ($ftype != "image/gif" && $ftype != "image/jpeg")
    {
    $error = "File type not supported";
    $problem = TRUE;
    }
    else
    {
    if (file_exists($dest))
    {
    $error = "File already exists. (".$dest.")";
    $problem = TRUE;
    }
    else
    {
    copy ($tmpfile,$dest);
    $problem = FALSE;
    }
    }
    }

    return array($problem, $error);
    }
    ?>
    </body>
    </html>
    ##########################

    Comment

    Working...