Solved

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

Posted on 2016-11-19
19
16 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 500 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
 
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now