-4

I am trying to upload many files in a form but it is saving one file out of four images.

$mypic = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
$id = $_POST['name'];

if(($type=="image/jpeg")||($type=="image/jpg")||($type=="image/bmp"))
{
$directory = "profiles/$id/images";
mkdir($directory, 0777,true);

move_uploaded_file($temp,"{$directory}/$mypic");
<form name="upload.php" enctype="multipart/form-data" method="post">
 <input type='file' name='upload'><br/>
 <input type='file' name='upload'><br/>
 <input type='file' name='upload'><br/>
 <input type='file' name='upload'><br/>
 <input type='submit' name='submit'/><br/>
 </form>
Community
  • 1
  • 1

5 Answers5

0

rename your inputs and use the corresponding index

<input type='file' name='upload1'>
$mypic = $_FILES['upload1']['name'];

<input type='file' name='upload2'>
$mypic = $_FILES['upload2']['name'];

etc.

chorn
  • 150
  • 2
0

Why dont you try giving different names to the inputs, 'image1', 'image2' etc etc, instead of 'upload'. Then in the PHP, you get the data of each image.

$mypic1 = $_FILES['image1']['name'];
$temp1 = $_FILES['image1']['tmp_name'];
$type1 = $_FILES['image1']['type'];
$id = $_POST['name'];

and

$mypic2 = $_FILES['image2']['name'];
$temp2 = $_FILES['image2']['tmp_name'];
$type2 = $_FILES['image2']['type'];

and so on with the others.

And to end it, just upload the files:

move_uploaded_file($temp,"{$directory}/$mypic1");
move_uploaded_file($temp,"{$directory}/$mypic2");
move_uploaded_file($temp,"{$directory}/$mypic3");
move_uploaded_file($temp,"{$directory}/$mypic4");

I hope it helps.

0

Welcome to StackOverflow,

You might want to change your HTML from

<form name="upload.php" enctype="multipart/form-data" method="post">
    <input type='file' name='upload'><br/>
    <input type='file' name='upload'><br/>
    <input type='file' name='upload'><br/>
    <input type='file' name='upload'><br/>
    <input type='submit' name='submit'/><br/>
</form>

to

<form name="upload.php" enctype="multipart/form-data" method="post">
    <input type='file' name='upload' multiple><br/>
    <input type='submit' name='submit'/><br/>
</form>

and your PHP to

<?php
    for ($i = 0; $i < count($_FILES['upload']['name']); $i++) {
        $myPictureName = $_FILES['upload']['name'][$i];
        $temporaryPath = $_FILES['upload']['tmp_name'][$i];
        $fileType = mime_content_type($temporaryPath); // not sure if this works
        $id = $_POST['name'];

        if ($fileType == "image/jpeg" OR $fileType == "image/png" OR $fileType == "image/bmp") {
            $fileDirectory = "profiles/" . $id . "/images";
            mkdir($fileDirectory, 0777, true); // you really shouldn't be making it 777. Just saying.
            move_uploaded_file($temporaryPath, $fileDirectory . "/" . $myPictureName);
        }
    }
?>

note that this is untested PHP code that I coded blindly

0

Here is your code.

This is html form to upload image

<form action="create_photo_gallery.php" method="post"   enctype="multipart/form-data">
<table width="100%">
    <tr>
        <td>Select Photo (one or multiple):</td>
        <td><input type="file" name="files[]" multiple/></td>
    </tr>
    <tr>
        <td colspan="2" align="center">Note: Supported image format: .jpeg, .jpg, .png, .gif</td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input type="submit" value="Create Gallery" id="selectedButton"/></td>
    </tr>
</table>
</form>

And Here is php submit function

extract($_POST);
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
        {
            $file_name=$_FILES["files"]["name"][$key];
            $file_tmp=$_FILES["files"]["tmp_name"][$key];
            $ext=pathinfo($file_name,PATHINFO_EXTENSION);
            if(in_array($ext,$extension))
            {
                if(!file_exists("photo_gallery/".$txtGalleryName."/".$file_name))
                {
                    move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$file_name);
                }
                else
                {
                    $filename=basename($file_name,$ext);
                    $newFileName=$filename.time().".".$ext;
                    move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$newFileName);
                }
            }
            else
            {
                array_push($error,"$file_name, ");
            }
        }
Atanu Mondal
  • 1,714
  • 1
  • 24
  • 30
Web Artisan
  • 1,870
  • 3
  • 23
  • 33
0

You can use same input name of array type adding multiple attribute to it. Like name='upload[]'. And, in submit page, you can restrict it to total of 4 images.

<form name="upload.php" enctype="multipart/form-data" method="post">
    <input type='file' name='upload[]' multiple><br/>
    <input type='submit' name='submit'/><br/>
</form>

upload.php

<?php
for($i=0;$i>4;$i++)
{ 

    $image_name = $_FILES['upload']['name'][$i];
    $mypic = $_FILES['upload']['name'][$i];
    $temp = $_FILES['upload']['tmp_name'][$i];
    $type = $_FILES['upload']['type'][$i];
    $id = $_POST['name'];
    if(($type=="image/jpeg")||($type=="image/jpg")||($type=="image/bmp"))
    {
        $directory = "profiles/".$id."/images";
        mkdir($directory, 0777,true);   

        if(move_uploaded_file($_FILES['upload']['tmp_name'][$i],$directory."/".$image_name))
        {
          // Insert Query
        }
    }
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77