How can I upload multiple pictures to my web page with php?

How can I upload multiple pictures to my web page with php?
LVL 1
BRMarketingAsked:
Who is Participating?
 
Marco GasiFreelancerCommented:
This is the markup for upload files: you have to insert it in your form
<form method="post" action="upload.php">
<input name="pictures[]" multiple="true" type="file">
</form>

Open in new window


The files will be uploaded to the script upload.php within the array $_FILES. Here a really basic code for upoad.php
$valid_formats = array(
        'jpg',
	'png',
	'gif'
)
if (isset($_FILES['pictures']['name']) && !empty($_FILES['pictures']['name'])){
	foreach ($_FILES['pictures']['name'] as $f => $name) {
		// Loop $_FILES to process all files
		if ($_FILES['pictures']['error'][$f] == 4) {
			$message[] = 'Unknown error!'; // 	continue; // Skip file if any error found
		}
		if ($_FILES['pictures']['error'][$f] == 0) {
			if ($_FILES['pictures']['size'][$f] > $max_file_size) {
				$message[] = "$name is too large!."; //	continue; // Skip large files
			} elseif (!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
				$message[] = "$name is not a valid format"; //	continue; // Skip invalid file formats
			} else { // No error found! Move uploaded files
				if (move_uploaded_file($_FILES['pictures']['tmp_name'][$f], 'upload/' . $name)) {
					$files[] = strtolower($name);
				}
			}
		}
	}
}

Open in new window

0
 
BRMarketingAuthor Commented:
Dear Marco Gasi,
Thank you very much.

What is the most secure way to do this?

Is this code is enough to secure the server while uploading files?

If this code is basic,
Do you have the more comlex and secure code for image upload?
0
 
Marco GasiFreelancerCommented:
As far as I know, that code is secure enough. It is basic because, for instance, I sometime prefer to change the picture name, or I want to create thumbnails and so on. Since you are uploading images storing them on the server I don't think there are security risk.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
BRMarketingAuthor Commented:
Thank yo very much Marco Gasi
0
 
BRMarketingAuthor Commented:
Thank you
0
 
Marco GasiFreelancerCommented:
You're welcome!
0
 
BRMarketingAuthor Commented:
Dear Marco,
Something wrong with the code?

On this bold line, php gives error

$valid_formats = array(
        'jpg',
      'png',
      'gif'
)
if (isset($_FILES['pictures']['name']) && !empty($_FILES['pictures']['name'])){
0
 
Marco GasiFreelancerCommented:
Did you change the name of your input element?
0
 
BRMarketingAuthor Commented:
I changed nothing, I just copied and pasted the code
0
 
Marco GasiFreelancerCommented:
What is the error message?
0
 
Marco GasiFreelancerCommented:
And can you show me your whole script?
0
 
BRMarketingAuthor Commented:
please see attached file, on the line 19 th, there is a problem.

it didn't show the page at all.
upload.php
0
 
BRMarketingAuthor Commented:
I found it :)
its a semicolon
0
 
Marco GasiFreelancerCommented:
I can't find the semicolon but I'm happy you have found the problem :)
0
 
BRMarketingAuthor Commented:
after this,
$valid_formats = array(
        'jpg',
      'png',
      'gif'
)  
a semicolon was needed
0
 
Marco GasiFreelancerCommented:
Wooops, I'm sorry!
0
 
Marco GasiFreelancerCommented:
Hi Braveheartli. I saw you posted another question about the same argument: did something go wrong with my solution?
0
 
BRMarketingAuthor Commented:
Sorry Marco,
I couldn't make it work.

firs it was the button missing? Then I put a submit button, but it didn't upload anyway. I'm sorry. Thank you anyway.
you taught me a lot. ( I learn a lot from you )
0
 
Marco GasiFreelancerCommented:
Yes, I didn't add the button because I thought the code would have been inserted in an existing form so I wanted only to show you the input element.
Anyway, that's strange because that ia piece of code I extracted by a my working script using in one of my sites...

I saw you prefer to upload each image separately, so I leave that to Julian, but the only thing I can think to is that maybe you don't have created the directory 'uoload' in your server (path is relative), so the upload fails because php doesn't create automatically a directory if this doesn't exist.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.