2013-11-26 38 views
0

我很新的PHP,並試圖做一個多圖像上傳表單(我正在循環進程)。由於某種原因,我有這個警告(警告:結束()期望參數1是數組,字符串在31行),這是這條線($temp = ($_FILES["file"]["name"][$i]);)警告:結束()期望參數1是數組,字符串上給出的字符串

請給我一些幫助。另外,我試圖確保圖像文件在上傳之前是正確的格式,所以我必須循環一次以確保它們是正確的格式,然後再次循環它們以便上傳? Ps。忽略SQL注入問題,稍後再添加。由於 腓

<?php 
ini_set('display_errors', 1); error_reporting(E_ALL); 

ob_start(); 
session_start(); 
include 'connect.php'; 



if ($_POST) 
{ 
//get form data 



$Listingname = addslashes(strip_tags($_POST['Listingname'])); 
$Location = addslashes(strip_tags($_POST['Location'])); 
$nobed = addslashes(strip_tags($_POST['nobed'])); 
$zip = addslashes(strip_tags($_POST['zip'])); 
$price = ($_POST['price']); 
$username=($_POST[$_SESSION['username']]); 

if (!$Listingname||!$nobed||!$nobed||!$zip||!$price) 
    echo "Please fill out all fields"; 
    else 

    {$allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $temp = $_FILES["file"]["name"]; 
    for($i=0;$i<count($temp);$i++) 
$temp = ($_FILES["file"]["name"][$i]); 
$extension = end($temp); 
if ((($_FILES["file"]["type"][$i] == "image/gif") 
|| ($_FILES["file"]["type"][$i] == "image/jpeg") 
|| ($_FILES["file"]["type"][$i] == "image/jpg") 
|| ($_FILES["file"]["type"][$i] == "image/pjpeg") 
|| ($_FILES["file"]["type"][$i] == "image/x-png") 
|| ($_FILES["file"]["type"][$i] == "image/png")) 
&& ($_FILES["file"]["size"][$i] < 400000) 
&& in_array($extension, $allowedExts)) 
    { 
    if ($_FILES["file"]["error"][$i] > 0) 
    { 
    echo "Return Code: " . $_FILES["file"]["error"][$i] . "<br>"; 
    } 
    else 
    { 
    echo "Upload: " . $_FILES["file"]["name"][$i] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"][$i] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"][$i]/1024) . " kB<br>"; 
    echo "Temp file: " . $_FILES["file"]["tmp_name"][$i] . "<br>"; 

    if (file_exists("upload/" . $_FILES["file"]["name"][$i])) 
     { 
     echo $_FILES["file"]["name"][$i] . " already exists please add another file, or change the. "; 
     } 
    else 

     { 
     $photo=$_FILES["file"]["name"][$i]; 
     move_uploaded_file($_FILES["file"]["tmp_name"][$i], 
     "upload/$photo"); 
     echo "Stored in: " . "upload/" . $_FILES["file"]["name"][$i]; 
     } 
    } 
    } 

else 

{ 
    echo "Invalid file" and die("Can not load picture"); 
    } 



    { 
     $username=$_SESSION['username']; 

      //register into database 
      mysqli_query($con,"INSERT INTO Listing (username,Listingname,Location,nobed,zip,price,pic1) VALUES 
       ('$username','$Listingname','$Location','$nobed','$zip','$price','$photo');") or die(mysqli_error()); 


      echo "Listing Added"; 

    } 
     } 

    } 



else 
{ 

?> 

<form action="Submitlisting5.php" method="post" 
enctype="multipart/form-data"> 
Listing Name:<br /> 
<input type='text' name='Listingname'><p /> 
Location:<br /> 
<input type='text' name='Location'><p /> 
Number of Beds:<br /> 
<input type='test' name='nobed'><p /> 
Zip:<br /> 
<input type='text' name='zip'><p /> 
Price:<br /> 
<input type='text' name='price'><p /> 


<label for="file">Pic1(File must be exceed 4mb):</label> 
<input type="file" name="file[]" id="file[]"><br> 
<label for="file">Pic2(File must be exceed 4mb):</label> 
<input type="file" name="file[]" id="file[]"><br> 
<br> 
<input type='submit' name='submit' value='Submit'> 
</form> 

<?php 

} 



?> 

只是嘗試這樣做,但現在它直接到無法上傳圖片

{$allowedExts = array("gif", "jpeg", "jpg", "png"); 

    for($i=0;$i<4;$i++){ 
$temp = ($_FILES["file"]["name"]); 
$extension = $temp; 
if ((($_FILES["file"]["type"][$i] == "image/gif") 
|| ($_FILES["file"]["type"][$i] == "image/jpeg") 
|| ($_FILES["file"]["type"][$i] == "image/jpg") 
|| ($_FILES["file"]["type"][$i] == "image/pjpeg") 
|| ($_FILES["file"]["type"][$i] == "image/x-png") 
|| ($_FILES["file"]["type"][$i] == "image/png")) 
&& ($_FILES["file"]["size"][$i] < 400000) 
&& in_array($extension, $allowedExts)) 

第三次試驗

for($i=0;$i<4;$i++){ 


if ((($_FILES["file"]["type"][$i] == "image/gif") 
|| ($_FILES["file"]["type"][$i] == "image/jpeg") 
|| ($_FILES["file"]["type"][$i] == "image/jpg") 
|| ($_FILES["file"]["type"][$i] == "image/pjpeg") 
|| ($_FILES["file"]["type"][$i] == "image/x-png") 
|| ($_FILES["file"]["type"][$i] == "image/png")) 
    && ($_FILES["file"]["size"][$i] < 400000)) 
+0

如果回顯出'$ _FILES [「file」] [「name」] [$ i]'的值,你會得到什麼?另外,'end()'不會讓你獲得文件擴展名,如果這就是你想要的;它會檢索數組中的最後一個元素,因此會出現錯誤。 – brandonscript

+0

如果我刪除結束(),它只是直接到無法上傳圖片沒有錯誤。不太確定我應該怎麼做,因爲它可以正常上傳1張圖片 – Benyaman

+0

我用我剛剛嘗試過的方式編輯了我的答案,現在沒有錯誤,但它直接跳到不能上傳圖片 – Benyaman

回答

0

問題是你將要覆蓋的變量:$ temp =

$temp = $_FILES["file"]["name"]; 
for($i=0;$i<count($temp);$i++) 
    $temp = ($_FILES["file"]["name"][$i]); 

此時,$ temp不再是一個數組。您需要重命名上面最後一行(以及引用該變量的任何其他地方...)的$ temp變量,而不是第一個變量。

+0

但是,如果我刪除第一組$ temp,我得到注意:未定義的變量:臨時警告:結束()期望參數1爲數組,空給出,如果我刪除第二個$臨我得到注意:未定義的偏移量:2行,31,32,33,34,35,36,37 – Benyaman

+0

我剛剛嘗試過沒有重複,但現在沒有錯誤,它只是直接進行無法上傳圖片 – Benyaman

0

只是設法整理出來,與多個圖像上傳與驗證圖像類型。

<?php 
ini_set('display_errors', 1); error_reporting(E_ALL); 

ob_start(); 
session_start(); 
include 'connect.php'; 



if ($_POST) 
{ 
//get form data 



$Listingname = addslashes(strip_tags($_POST['Listingname'])); 
$Location = addslashes(strip_tags($_POST['Location'])); 
$nobed = addslashes(strip_tags($_POST['nobed'])); 
$zip = addslashes(strip_tags($_POST['zip'])); 
$price = ($_POST['price']); 
$username=($_POST[$_SESSION['username']]); 

if (!$Listingname||!$nobed||!$nobed||!$zip||!$price) 
    echo "Please fill out all fields"; 
    else 



    for($i=0;$i<3;$i++){ 


if ((($_FILES["file"]["type"][$i] == "image/gif") 
|| ($_FILES["file"]["type"][$i] == "image/jpeg") 
|| ($_FILES["file"]["type"][$i] == "image/jpg") 
|| ($_FILES["file"]["type"][$i] == "image/pjpeg") 
|| ($_FILES["file"]["type"][$i] == "image/x-png") 
|| ($_FILES["file"]["type"][$i] == "image/png")) 
    && ($_FILES["file"]["size"][$i] < 400000)) 


    if ($_FILES["file"]["error"][$i] > 0) 
    { 
    echo "Return Code: " . $_FILES["file"]["error"][$i] . "<br>"; 
    } 
    else 
    { 
    echo "Upload: " . $_FILES["file"]["name"][$i] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"][$i] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"][$i]/1024) . " kB<br>"; 
    echo "Temp file: " . $_FILES["file"]["tmp_name"][$i] . "<br>"; 

    if (file_exists("upload/" . $_FILES["file"]["name"][$i])) 
     { 
     die($_FILES["file"]["name"][$i] . " already exists please add another file, or change the name "); 
     } 

    else 

     { 
     $photo=$_FILES["file"]["name"][$i]; 
     move_uploaded_file($_FILES["file"]["tmp_name"][$i], 
     "upload/$photo"); 
     echo "Stored in: " . "upload/" . $_FILES["file"]["name"][$i]; 
     } 
    } 


else 

{ 
    echo "Invalid file" and die("Can not load picture"); 
    } 



    { 
     $username=$_SESSION['username']; 

      //register into database 
      mysqli_query($con,"INSERT INTO Listing (username,Listingname,Location,nobed,zip,price,pic1) VALUES 
       ('$username','$Listingname','$Location','$nobed','$zip','$price','$photo');") or die(mysqli_error()); 


      echo "Listing Added"; 

    } 
     } 


} 


else 
{ 

?> 

<form action="Submitlisting5.php" method="post" 
enctype="multipart/form-data"> 
Listing Name:<br /> 
<input type='text' name='Listingname'><p /> 
Location:<br /> 
<input type='text' name='Location'><p /> 
Number of Beds:<br /> 
<input type='test' name='nobed'><p /> 
Zip:<br /> 
<input type='text' name='zip'><p /> 
Price:<br /> 
<input type='text' name='price'><p /> 


<label for="file">Pic1(File must be exceed 4mb):</label> 
<input type="file" name="file[]" id="file[]"><br> 
<label for="file">Pic2(File must be exceed 4mb):</label> 
<input type="file" name="file[]" id="file[]"><br> 
<label for="file">Pic3(File must be exceed 4mb):</label> 
<input type="file" name="file[]" id="file[]"><br> 
<br> 
<input type='submit' name='submit' value='Submit'> 
</form> 

<?php 

} 



?> 
相關問題