2016-06-08 42 views
1

我看到的大多數問題都是使用一個圖像輸入。但是如果你有2個或3個或更多?一個表單中的多個圖像輸入?

我的腳本爲一個圖像輸入工作,但我想在我的窗體中有一個單一圖像輸入和一個多個圖像輸入。我正在測試我的表單上有2個單一圖像輸入,但無法工作。

下面是一個單圖像輸入(假設它是一個<形式>標記內)的HTML:

<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中時,代碼不起作用。沒有錯誤信息或任何東西。該頁面爲空白。

順便說一句,here is my other question在那裏我試圖添加一個多圖像上傳輸入到我的表單與一個已經工作的單圖上傳輸入。

+0

可能的重複[參考 - 這個錯誤在PHP中意味着什麼? - 什麼都看不到。該頁面是空白的。](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851) – CBroe

回答

1

如果條件錯誤關閉。

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 

} 
+0

你檢查答案? – Saty

+0

我有,但沒有改變。沒有錯誤。在我提出您所做的更改後,仍爲空白頁。 – thomas

+0

使用'ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL);'檢查頁面 – Saty

1

這會爲你工作,與此

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