Jump to content

foreach around code for uploading files


Meridius

Recommended Posts

Hello,

 

I'm still a bit of a n00b by most standards but learning every day, currently I am working on a script to let the user upload images. Uploading a single file is no problem, that was easily ripped from w3schools. I also got a bit of code that re-sizes images to a maximum of 1920x1080.

As the input type also has a "multiple" these days i wanted to make use of that. Easier for the user to use instead of selecting every image seperetly. So instead of just name="file" i made it into:

<input type="file" name="file[]" id="file" multiple=""/> 

 

Now if i do this:

foreach ($_FILES["file"]["name"] as $filename) {
    echo '<li>' . $filename . '</li>';
}

It shows the filenames nicely.

 

The part on which i keep failing is having to put it around the following block of code:

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

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  
  include('ResizeImage.php');
  $image = new ResizeImage();
  $image->load("upload/" . $_FILES["file"]["name"]);
  $image->resize(1920,1080);
  $image->save("upload/" . $_FILES["file"]["name"]);
  
      }
    }
  }
else
  {
  echo "Invalid file";
  }

Like i said, the uploading is ripped from w3schools. I tried several things but my knowledge simply does not reach that far or i just can't see the solution after trying stuff.

 

Any pointers or suggestions would be greatly appreciated!

 

Kind regards//Thanks in advance,

Erik

Link to comment
Share on other sites

Which part are you stuck on exactly?

 

Here is a good way to iterate through $_FILES when you have multiple uploads:

for($i = 0; $i < count($_FILES['file']['name']); $i++)
{
echo "Name: {$_FILES['file']['name'][$i]}<br />
Type: {$_FILES['file']['type'][$i]}<br />
Temp name: {$_FILES['file']['tmp_name'][$i]}<br />
Error: {$_FILES['file']['error'][$i]}<br />
Size: {$_FILES['file']['size'][$i]}<br />";

}

 

This way, you can access all of the properties on each iteration without having to have loops for each one, if that makes sense.

Link to comment
Share on other sites

You good sir, made my day!

 

It was indeed just a simple matter of using the for loop and putting [$i] behind every piece of code that called the $_FILES().

 

Final working code:

<?php

include('ResizeImage.php');

for($i = 0; $i < count($_FILES['file']['name']); $i++)
{
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"][$i]));
if ((($_FILES["file"]["type"][$i] == "image/gif")
|| ($_FILES["file"]["type"][$i] == "image/jpeg")
|| ($_FILES["file"]["type"][$i] == "image/pjpeg"))
&& ($_FILES["file"]["size"][$i] < 200000000)
&& 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. ";
	  }
	else
	  {
	  move_uploaded_file($_FILES["file"]["tmp_name"][$i],
	  "upload/" . $_FILES["file"]["name"][$i]);
	  echo "Stored in: " . "upload/" . $_FILES["file"]["name"][$i];
	  
	  $image = new ResizeImage();
	  $image->load("upload/" . $_FILES["file"]["name"][$i]);
	  $image->resize(1920,1080);
	  $image->save("upload/" . $_FILES["file"]["name"][$i]);
	  
	  }
	}
  }
else
  {
  echo "Invalid file";
  }
}
  
?>

 

Stupid of me that i did not see it, sometimes i just do not see the trees through the forest in a matter of speech hehe.

Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.