0

I am pretty new to php, and trying to do a multiple images upload form( i am looping the process). For some reason i am having this warning(Warning: end() expects parameter 1 to be array, string given on line 31),Which is this line ($temp = ( $_FILES["file"]["name"][$i]);)

please give me some help. Also, i am trying to make sure the image files are the right format and such before uploading them, so would i have to loop them once to make sure they are the correct format, then loop them again for the upload? Ps. Ignore the SQL injection issue,will add those later. Thanks Php

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

}



?>

Just tried this, but now it goes straight to Can not upload picture

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

Third Trial

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))
Benyaman
  • 451
  • 2
  • 10
  • 25
  • if you echo out the value of `$_FILES["file"]["name"][$i]` what do you get? Also, `end()` won't get you the file extension, if that's what you're looking for; it retrieves the last element in an array, hence the error you're seeing. – brandonscript Nov 26 '13 at 06:54
  • If i remove the end(), it just goes straight to Can not upload picture with no error. Not very sure what i should do at all, because it works all fine with uploading 1 image – Benyaman Nov 26 '13 at 07:30
  • I edited my answer with what i just tried, there is no error now but it jumps straight to can not upload pictures – Benyaman Nov 26 '13 at 07:49
  • I just managed to get the codes to work. :D – Benyaman Nov 26 '13 at 08:20

2 Answers2

0

The problem is you are overwriting your variable:$temp =

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

At this point, $temp is no longer an array. You need to rename the $ temp variable on the last line above (and anywhere else referencing that variable... And not the first one).

dave
  • 62,300
  • 5
  • 72
  • 93
  • But if i remove the first set of $temp, i am getting Notice: Undefined variable: temp Warning: end() expects parameter 1 to be array, null given, if i remove the second $temp i am getting Notice: Undefined offset: 2 in line,31,32,33,34,35,36,37 – Benyaman Nov 26 '13 at 07:23
  • I just tried it with no duplicate, but now there is no error, it just goes straight to Can not upload pictures – Benyaman Nov 26 '13 at 07:48
0

Just managed to sort it out, with multiple images upload with verifying image type.

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

}



?>
Benyaman
  • 451
  • 2
  • 10
  • 25