Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-19
19
Medium Priority
?
38 Views
Last Modified: 2016-11-21
How can I upload multiple pictures to my web page with php?
0
Comment
Question by:Braveheartli
  • 10
  • 9
19 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 41894594
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
 
LVL 1

Author Comment

by:Braveheartli
ID: 41894680
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41894687
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 1

Author Comment

by:Braveheartli
ID: 41894688
Thank yo very much Marco Gasi
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41894689
Thank you
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41894690
You're welcome!
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41895375
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41895410
Did you change the name of your input element?
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41895414
I changed nothing, I just copied and pasted the code
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41895423
What is the error message?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41895424
And can you show me your whole script?
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41895425
please see attached file, on the line 19 th, there is a problem.

it didn't show the page at all.
upload.php
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41895428
I found it :)
its a semicolon
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41895434
I can't find the semicolon but I'm happy you have found the problem :)
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41895436
after this,
$valid_formats = array(
        'jpg',
      'png',
      'gif'
)  
a semicolon was needed
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41895440
Wooops, I'm sorry!
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41895501
Hi Braveheartli. I saw you posted another question about the same argument: did something go wrong with my solution?
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41895512
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41895540
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question