Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

uploading multiple image with php

I need to let my users to upload multiple images to my web site,

do you have the code for uploading multiple image with php?

I don't show error messages on my page for the security risk, they are off by default.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Images are uploaded in the $_FILES array.

Assuming you are uploading your files with standard HTML file inputs there are two approaches
Approach 1 - you name each file input
<input type="file" name="image1" />
<input type="file" name="image2" />

Open in new window

This will result in a $_FILES array that looks like this
Array
(
    [image1] => Array
        (
            [name] => background5.jpg
            [type] => image/jpeg
            [tmp_name] => C:\Windows\Temp\php4C53.tmp
            [error] => 0
            [size] => 309187
        )

    [image2] => Array
        (
            [name] => eetest.jpg
            [type] => image/jpeg
            [tmp_name] => C:\Windows\Temp\php4C54.tmp
            [error] => 0
            [size] => 47336
        )
)

Open in new window

Approach 2 - use the multiple attribute on the <input> control. This allows you to select multiple files and assign them to a single <input>
<input name="file[]" type="file" multiple /> 

Open in new window

This will result in a $_FILES array that looks like this
Array
(
    [file] => Array
        (
            [name] => Array
                (
                    [0] => background2.jpg
                    [1] => background3.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => C:\Windows\Temp\php6B9E.tmp
                    [1] => C:\Windows\Temp\php6B9F.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 8989
                    [1] => 6172
                )

        )
)

Open in new window


Use the first approach when you want to request specific images for a specific purpose - the named inputs allows you to map the image to its purpose.
The second approach is if you are providing something like a gallery - there can be any number of images in any order.

Assuming you go with Approach 1. Then you would need PHP code that processes the first $_FILES array

foreach($_FILES as $file) {
   // Change path if you want to use a specific name
   $dest = "/path/to/images/" . $file['name'];
   uploadFile($file, $dest);
}

function uploadFile($file, $destination, $mimetypes=false, $size = 0) 
{
   // check that the file is within the required parameters
   // Check size, mimetype here
   if ($size > 0 && filesize($file['tmp_name']) > $size) {
      return false;
   }
   if (is_array($mimetypes)) {
      $exif = exif_imagetype($file['tmp_name'];
      $valid = false;
      foreach($mimetypes as $type) {
         if ($exif === $type) {
            $valid = true;
            break;
         }
      }
   }
   if (!$valid) {
      return false;
   }
   // file is valid
   return move_uploaded_file($file['tmp_name'], $destination);
}

Open in new window

Avatar of BR

ASKER

Dear Julian,
thank you, As you guest, I prefer the first approach.

Do you have complete code or sample for this?

I need to let my users upload only jpeg and .gif files and I need to limit the file size.
do you have complete code for this?
Avatar of BR

ASKER

Let me try it please.
I will let you know after i try it. Thank you
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is my teaching example showing how to use HTML5 to send a multiple-file upload to your server.
<?php // demo/upload_multiple_html5_example.php
/**
 * This script demonstrates the HTML5 document that you need to use
 * for multiple file uploads.  The "action=" script would be in a
 * separate document.
 */
error_reporting(E_ALL);


/**
 * Activate these lines to see the request variables
 *
var_dump($_POST);
var_dump($_FILES);
 *
 */

// CREATE OUR WEB PAGE IN HTML5 FORMAT
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
/* STYLE SHEET HERE */
</style>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
/* JQUERY CODE HERE */
});
</script>

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>
</head>
<body>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

<form enctype="multipart/form-data" method="post">
<input type="file" multiple name="my_files[]" />
<input type="submit" />
</form>

</body>
</html>
HTML5;


// RENDER THE WEB PAGE
echo $htm;

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

Dear Ray,
thank you very very much.

Dear Julian Hansen, I thank you too. Somehow, I couldn't upload files with your code. I'm sure it's my fault.

But Ray's code works, perfectly fine.
Avatar of BR

ASKER

Dear Ray Paseur,
I use your code, I can see what I uploaded on the page.

echo '<br/>See the file: <a target="_blank" href="' . $my_file_url . '">' . $my_file_url . '</a><br/>' . PHP_EOL;

Since it's more than one picture, how can I insert the link into my database?

I think I can insert the link into database inside the foreach loop that you use to print out the link on the page.

if I do this, would it work inside the foreach loop?

$sql = "INSERT INTO ilan (uid,imageurl) VALUES ('$uid','$my_file_url');"
but there is more than one image url? How can I do this?

Normally I insert all the data in one row whit where clause. ( Where uid='$uid'; )
Should I create a lookup table to insert the picture urls?
Avatar of BR

ASKER

Dear Julian,
Yes, I tried. Unfortunately, I couldn't make it work
That might be a bandwidth thing .

The sample requires that the first image is a JPEG and the second is a GIF
See screenshot below
User generated image
Just checking in this post you said
thank you, As you guest, I prefer the first approach.

Ray's solution uses a single input with the multiple attribute - which was the second approach I suggested.

Which one are you wanting to use the first (individual inputs) or the second (single input with multiple attribute)?
Avatar of BR

ASKER

Dear Julian,
I somehow manage it. Ray's code is working and I do not have deep knowledge about programming. I'm sure yours is working too.
I will use Ray's code for now. But I have to work on it.

I inserted the link of the pictures to my database, so that everything seems fine now. I will split the points.
Avatar of BR

ASKER

thank you both.