Solved

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

Posted on 2016-11-19
19
12 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 30

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 30

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 30

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 30

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Expert Comment

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

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 30

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 30

Expert Comment

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

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 30

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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 …

708 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

16 Experts available now in Live!

Get 1:1 Help Now