Solved

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

Posted on 2016-11-19
19
32 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Displaying a google org chart from php/mysql code 27 47
MySQL 6 45
Format Date 7 26
Considerations PHP using multiple locations 9 32
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

737 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