简体   繁体   中英

How to upload a photo but a different folder?

I have this code

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

and i want php like this, but I do not know how to set it up

$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);

So if i upload image at red, image will be saved at folder red. if upload at white, will saved at folder white. I do not want to be separated , I want to use one button cause this image will saved based on the data item. And the data will be stored in MySQL, dependent,the color. Because if i use for ($i = 0; $i < count ($_FILES['files']['name']); $i++) I do not understand how to separate them by color.

If we imagine, I wanted to include photo NIKIPredator shoes , Then there are three colors black,red and white. So I upload photo but the image, separated from storage. Can it?


EDITED I use php like this


<?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.";
}

?>

And HTML like this

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

To do this, we're going to want to differentiate the input s.

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

If a user uploads via the white input , $_FILES['white'] , $_FILES['white']['name'] and $_FILES['white']['size'] will be initialized.

We can turn the following pseudo code:

$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];

Into:

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'];
}

I'll let you figure out how to save the various files in their respective directories. Just be careful to make sure your variables are defined before you use them.

I haven't tested the code above. Please excuse any errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM