在数据库中上传四个图像

时间:2016-04-27 13:53:58

标签: php mysql

我正在尝试上传表单中的多个文件,但它正在保存四个图像中的一个文件。



$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>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:0)

重命名输入并使用相应的索引

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

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

答案 1 :(得分:0)

为什么不尝试为输入提供不同的名称,'image1''image2'等等,而不是'upload'。然后在PHP中,您将获得每个图像的数据。

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

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

等等。

要结束它,只需上传文件:

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

我希望它有所帮助。

答案 2 :(得分:0)

欢迎使用StackOverflow,

您可能想要从

更改HTML
<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>

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

和你的PHP

<?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);
        }
    }
?>

请注意,这是我盲目编码的未经测试的PHP代码

答案 3 :(得分:0)

这是你的代码。

这是上传图片的html表单

<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>

这是php提交功能

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, ");
            }
        }

答案 4 :(得分:0)

您可以使用array type添加multiple属性的相同输入名称。像name='upload[]'一样。并且,在提交页面中,您可以将其限制为总共4个图像。

<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
        }
    }
}
?>