如何上传照片但不同的文件夹?

时间:2016-02-03 02:04:50

标签: javascript php html ajax

我有这段代码

<div class="item1">
    <form action="" method="post" enctype="multipart/form-data">

        <label>White</label>
        <input type="file" name="files[]" multiple="multiple" accept="image/*">


        <label>Black</label>
        <input type="file" name="files[]" multiple="multiple" accept="image/*">


        <label>Red</label>
        <input type="file" name="files[]" multiple="multiple" accept="image/*">

        <input type="submit" value="Upload">

    </form>
</div>

我希望像这样的PHP,但我不知道如何设置它

$namewhite = $_FILES['photoimg']['name'][white];
$sizewhite = $_FILES['photoimg']['size'][white];

$nameblack = $_FILES['photoimg']['name'][black];
$sizeblack = $_FILES['photoimg']['size'][black];

$namered = $_FILES['photoimg']['name'][red];
$sizered = $_FILES['photoimg']['size'][red];


$path1 = "white/";
$path2 = "black/";
$path3 = "red/";

if(move_uploaded_file($tmp, $path1.$actual_image_name1))
if(move_uploaded_file($tmp, $path2.$actual_image_name2))
if(move_uploaded_file($tmp, $path3.$actual_image_name3))

$query = "INSERT INTO upload_image (id_item, color, url)VALUES('$id_item', '$color', '$url')";
$result8 = $mysqli->query($query);

因此,如果我上传红色图像,图像将保存在红色文件夹中。如果上传为白色,将保存在文件夹白色。我不想分开,我想使用一个按钮,因为这个图像将根据数据项保存。并且数据将存储在MySQL中,依赖于颜色。因为如果我用于($i = 0; $i < count ($_FILES['files']['name']); $i++)我不明白如何用颜色分隔它们。

如果我们想象,我想要包括照片NIKIPredator鞋子,那么有三种颜色黑色,红色和白色。所以我上传照片但图片与存储分开。可以吗?

已编辑我像这样使用php

<?php

if (isset($_POST['submit'])){

for($i=0;$i<4;$i++){
    echo "<b>File".($i+1).".</b><br>";  
    if ((($_FILES["userfile"]["type"][$i] == "image/gif")
    || ($_FILES["userfile"]["type"][$i] == "image/jpeg")
    || ($_FILES["userfile"]["type"][$i] == "image/pjpeg"))
    && ($_FILES["userfile"]["size"][$i] < 100000))
      {
          if ($_FILES["userfile"]["error"][$i] > 0)
            {
              echo "File Error :  " . $_FILES["userfile"]["error"][$i] . "<br />";
            }else {

              if (file_exists("images/".$_FILES["userfile"]["name"][$i]))
                {
                   echo "<b>".$_FILES["userfile"]["name"][$i] . " already exists. </b>";
                }else
                {
                   move_uploaded_file($_FILES["userfile"]["tmp_name"][$i],"images/". $_FILES["userfile"]["name"][$i]);
                   echo "Stored in: " . "images/" . $_FILES["userfile"]["name"][$i]."<br />";
                   ?>
                     Uploaded File:<br>
                     <img src="images/<?php echo $_FILES["userfile"]["name"][$i]; ?>"  width="100"
                      height="100" alt="Image path Invalid" >
                  <?php
               }
            }
          }else
          {
           echo "Invalid file detail<br> file type ::".$_FILES["userfile"]["type"][$i]." , file size::: ".$_FILES["userfile"]["size"][$i];
        }
        echo "<br>";
  }
}else{   
    echo "File details not avaliable.";
}

?>

像这样的HTML

  <form action="multiple_file_upload.php" method="post" onSubmit="return validate()"
  enctype="multipart/form-data"  >
    <table align="center"  >
      <tr>
        <td><label for="file1">File 1:</label></td>
        <td><input name="userfile[]" type="file"  id="file" /></td>
      </tr>
      <tr>
        <td><label for="file2">File 2:</label></td>
        <td><input name="userfile[]" type="file" /></td>
      </tr>
      <tr>
        <td><label for="fil3">File 3:</label></td>
        <td><input name="userfile[]" type="file" /></td>
      </tr>
      <tr>
        <td><label for="file4">File 4:</label></td>
        <td><input name="userfile[]" type="file" /></td>
      </tr>   
      <tr>
        <td></td>
        <td><input type="submit" name="submit" value="Submit" /></td>
      </tr>
    <table>
 </form>

1 个答案:

答案 0 :(得分:1)

为此,我们想要区分input

<div class="item1">
    <form action="" method="post" enctype="multipart/form-data">

        <label>White</label>
        <!-- Changed name to 'white' -->
        <input type="file" name="white[]" multiple="multiple" accept="image/*">


        <label>Black</label>
        <!-- Changed name to 'black' -->
        <input type="file" name="black[]" multiple="multiple" accept="image/*">


        <label>Red</label>
        <!-- Changed name to 'red' -->
        <input type="file" name="red[]" multiple="multiple" accept="image/*">

        <input type="submit" value="Upload">

    </form>
</div>

如果用户通过white input上传,则$_FILES['white']$_FILES['white']['name']$_FILES['white']['size']将被初始化。

我们可以打开以下伪代码:

$namewhite = $_FILES['photoimg']['name'][white];
$sizewhite = $_FILES['photoimg']['size'][white];

$nameblack = $_FILES['photoimg']['name'][black];
$sizeblack = $_FILES['photoimg']['size'][black];

$namered = $_FILES['photoimg']['name'][red];
$sizered = $_FILES['photoimg']['size'][red];

分为:

if (isset($_FILES['white'])) {
    $namewhite = $_FILES['white']['name'];
    $sizewhite = $_FILES['white']['size'];
}

if (isset($_FILES['black'])) {
    $nameblack = $_FILES['black']['name'];
    $sizeblack = $_FILES['black']['size'];
}

if (isset($_FILES['red'])) {
    $redblack = $_FILES['red']['name'];
    $redblack = $_FILES['red']['size'];
}

我将让您弄清楚如何将各种文件保存在各自的目录中。在使用变量之前,请务必确保定义变量。

我还没有测试过上面的代码。请原谅任何错误。