M. Niyazi Alpay
M. Niyazi Alpay
M. Niyazi Alpay

I've been interested in computer systems since a very young age, and I've been programming since 2005. I have knowledge in PHP, MySQL, Python, MongoDB, and Linux.

 

about.me/Cryptograph

  • admin@niyazi.org
PHP Multiple Image Upload and Resizing

Hello, in the previous tutorial, I explained how to use the image upload and resizing class. In this tutorial, I'll show you how to upload multiple images using the same class.

First, let's create an empty HTML file and add the following code:

<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="resim[]" multiple>
  <input type="submit" value="Upload">
</form>

In the input field's name attribute, we used resim[], indicating that the name of the image input will be an array. This means that the value of this input will be sent to the upload.php file as an array variable containing multiple values.

Now, let's create the upload.php file and add the following code:

<?php
include 'resim.class.php';
$upload = new ResimIslem();
foreach ($_FILES["resim"]["name"] as $n => $name) {
  if (!empty($name)) {
    echo $upload->resim_upload(
      $_FILES["resim"]["name"][$n],
      $_FILES["resim"]["tmp_name"][$n],
      $_FILES["resim"]["error"][$n],
      'resim-dizini',
      'dosya ismi - ' . $n,
      1920,
      1200
    ) . "<br>";
  }
}
?>

Here, resim.class.php is the file containing our ResimIslem class. Since the value of the resim[] input comes as an array, we use a foreach loop to iterate over it. We assign the index number of the array to the $n variable and the file name to the $name variable. Within the loop, we check if the file name is not empty, then we call the resim_upload() function to upload each file individually.

The reason we use $_FILES["resim"]["name"][$n] is that $n starts from zero and increases by one in each iteration of the loop. So $_FILES["resim"]["name"][0] will give us the information of the first file, $_FILES["resim"]["name"][1] the second file, and so on. Using a loop allows us to increment this number and enable multiple upload functionality.

You may also want to read these

There are none comment

Leave a comment

Your email address will not be published. Required fields are marked *