Solved

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

Posted on 2016-11-19
19
33 Views
Last Modified: 2016-11-21
How can I upload multiple pictures to my web page with php?
0
Comment
Question by:Braveheartli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Webinar: Deploying MySQL in production 6/22 11am

Join Percona’s Senior Operations Engineer, Daniel Kowalewski as he presents Deploying MySQL in production on Thursday, June 22, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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