警告:end()期望参数1为数组,第31行给出字符串

时间:2013-11-26 06:45:37

标签: php file-upload php-5.4 multifile-uploader

我对php很新,并试图做一个多图像上传表单(我循环过程)。出于某种原因,我有这个警告(警告:end()期望参数1是数组,第31行给出的字符串),这一行是($temp = ( $_FILES["file"]["name"][$i]);)

请给我一些帮助。此外,我试图确保图像文件是正确的格式等,然后上传它们,所以我必须循环它们一次以确保它们是正确的格式,然后再次循环它们上传? 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))

2 个答案:

答案 0 :(得分:0)

问题是你要覆盖你的变量:$ temp =

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

此时,$ temp不再是数组。你需要重命名上面最后一行的$ temp变量(以及引用该变量的其他地方......而不是第一行)。

答案 1 :(得分: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

}



?>