Multiple File Uploads

I am trying to enable multiple file uploads but there is a problem with my code. It should validate each file and then upload to the specified directory. File uploads are not being validated or processed but the JavaScript notification for successful upload is triggered every time.
[php]<?php
//Define allowed file types
$allowedFileTypes = array (“csv”, “xls”, “xlsx”);
//Explode the file name using . as the delimiter
$temp = array(explode(".", $_FILES[“file”][“name”]));
//Count multiple files array
$filesCount = count($_File[“file”][“name”]);
//Obtain the file extension
$fileType = end($temp["$f"]);
//Loop through each file in array
$f=0;
while ($f < $filesCount)
{
if ((($_FILES[“file”][“type”]["$f"] == “text/csv”)
|| ($_FILES[“file”][“type”]["$f"] == “application/vnd.ms-excel”)
|| ($_FILES[“file”][“type”]["$f"] == “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”))
&& in_array ($fileType, $allowedFileTypes))
//Move the uploaded file to the uploads folder
{
move_uploaded_file($_FILES[“file”][“tmp_name”]["$f"], “…/uploads/” . $_FILES[“file”][“name”]["$f"]);
$f++;
}
else
{
echo “”;
die(‘Attemped to upload an invalid file type’);
}
}
//Confirm the file saved
$true=0;
for ($f=0; $f < $filesCount; $f++)
{
if (file_exists("…/uploads/" . $_Files[“file”][“name”]["$f"]) == true);
$true++;
}
if ($true=2)
{
echo “”;
}
else
{
echo “”;
}
?>[/php]

a quick glance through your code i see this:
[php]if ($true=2)[/php] This will always be true as you are assigning the value 2 to $true.
I suspect you wanted to test if the value of $true is indeed 2 then that would be:
[php]if ($true==2)[/php]

Hope that helps,
Red :wink:

Such a simple mistake! I’ve done a bit more tweaking and removed the “extension” validation. Now the files are only validated based on MIME types. Everything is working!
[php]<?php
//Define array value
$f=0;
//Count multiple files array
$filesCount = count($_FILES[“file”][“name”]);
//Loop through each file in array
while ($f < $filesCount)
{
if ((($_FILES[“file”][“type”]["$f"] == “text/csv”)
|| ($_FILES[“file”][“type”]["$f"] == “application/vnd.ms-excel”)
|| ($_FILES[“file”][“type”]["$f"] == “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”)))
//Move the uploaded file to the uploads folder
{
move_uploaded_file($_FILES[“file”][“tmp_name”]["$f"], “…/uploads/” . $_FILES[“file”][“name”]["$f"]);
$f++;
}
else
{
echo “”;
die(“”);
}
}
//Confirm the file saved
$true=0;
for ($f=0; $f < $filesCount; $f++)
{
if (file_exists("…/uploads/" . $_FILES[“file”][“name”]["$f"]) == true);
$true++;
}
if ($true==2)
{
echo “”;
}
else
{
echo “”;
}
?>[/php]

Happy days!! :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service