簡體   English   中英

一種形式中有多個圖像輸入?

[英]Multiple image inputs within one form?

我看到的大多數問題都與一個圖像輸入有關。 但是,如果您擁有2或3或更多,該怎么辦?

我的一個圖像輸入腳本起作用,但是我試圖在表單中包含一個單圖像輸入和一個多圖像輸入。 我正在測試在表單上有2個單圖像輸入,但無法正常工作。

這是一個單圖像輸入的HTML(假設它在<form>標記內):

  <input type="hidden" name="size" value="350000"/>
  <input type="file" name="schoollogo"/><br/>

PHP:

  $target = "images/logo/";
  $target = $target . basename( $_FILES['schoollogo']['name']);

  $schoollogo = $_FILES['schoollogo']['name'];

  //Writes the information to the database

  mysql_query("INSERT INTO Colleges (schoollogo) VALUES ('$schoollogo')") or die(mysql_error()) ;

  //Writes the logo to the server
  if (move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) {
      //Tells you if its all ok
      echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
  } else {
      //Gives and error if its not
      echo "Sorry, there was a problem uploading your file.";
  }

對於第二個單圖像輸入,我剛剛添加了第二個輸入代碼...

 <input type="hidden" name="size" value="350000"/>
 <input type="file" name="otherimage"/><br/>

然后將變量添加到PHP:

  $targetotherimage = "images/otherimage/";
  $targetotherimage = $targetotherimage . basename( $_FILES['otherimage']['name']);

  mysql_query("INSERT INTO Colleges (schoollogo,otherimage) VALUES ('$schoollogo','$otherimage')") or die(mysql_error()) ;

  //Writes the logo to the server
  if (move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) {

但是,當將該代碼添加到PHP中時,該代碼將無法工作。 沒有錯誤消息或任何東西。 頁面為空白。

順便說一句, 這是我的另一個問題 ,我正在嘗試將一個多圖像上傳輸入與一個已經正常工作的單圖像上傳輸入添加到表單中。

錯誤關閉if條件。

  if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) {
                                                                  ^^^^// In your code if condition close here

用作

  if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target) 

&& (move_uploaded_file($_FILES['otherimage']['tmp_name'],  $targetotherimage)))//if condition close here 
{
// rest of code

}

這將為您工作,用此替換您的表格

<form method="post" enctype="multipart/form-data" action="upload.php">
      <input type="file" name="file['schoollogo']" >
      <input type="file" name="file['otherimage']" >
      <input type="submit" value="submit">
</form>

您將以以下格式獲取文件:

Array
(
    [file] => Array
        (
            [name] => Array
                (
                    ['schoollogo'] => 1465386599_Location_Outline-36.png
                    ['otherimage'] => STONE_Website_v7E.jpg
                )

            [type] => Array
                (
                    ['schoollogo'] => image/png
                    ['otherimage'] => image/jpeg
                )
        ) 
) 

並在upload.php文件中

<?php 
if (isset($_FILES['file'])) 
{
     $schoollogo=$_FILES['file']['name']['schoollogo'];
     $target = "images/logo/".basename($schoollogo);;

     $otherimage = $_FILES['file']['name']['otherimage'];
     $targetotherimage = "images/otherimage/".basename($otherimage);

      $tmp_name_school = $_FILES['file']['tmp_name']['schoollogo'];
      $tmp_name_other = $_FILES['file']['tmp_name']['otherimage'];

      if ($tmp_name_school != "" && $tmp_name_other !== "")
      {
        //Upload the file 
        if(move_uploaded_file($tmp_name_school,$target) && move_uploaded_file($tmp_name_other, $targetotherimage)) 
        {
          mysql_query("INSERT INTO Colleges (schoollogo,otherimage) VALUES ('$schoollogo','$otherimage')") or die(mysql_error()) ;
          echo'Done!';
        }
        else
        {
            echo "Not Moved";
        }
      }
}   
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM