473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Uploading multiple files-opinion

58
Hi everybody !
I'd be grateful if anyone can point out to me what is missing to get following code working

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $filename = $_FILES['userfile']['name'];
  3. $tempname  = $_FILES['userfile']['tmp_name'];
  4. ------
  5. -------
  6. ?>
  7.  
  8. <form action="file-upload.php" method="post" enctype="multipart/form-data">
  9.   Send these files:<br />
  10.   <input name="userfile1" type="file" /><br />
  11.   <input name="userfile2" type="file" /><br />
  12.   <input type="submit" value="Submit" />
  13. </form>
  14.  
  15. foreach ($_FILES as $file) { 
  16.  
  17. if (move_uploaded_file($_FILES['userfile']['tmp_name'][$file], $uploadfile)) {
  18.  
  19. ..........
  20. ..........
  21.  
  22. ... }
[Please use CODE tags when posting source code. Thanks! --pbmods]

I also wonder if this would be enough to insert multiple files at once in a db.

My gratitude in advance !
Jul 8 '07 #1
6 2002
pbmods
5,821 Expert 4TB
Heya, Jankie.

Your inputs are going to populate $_FILES['userfile1'] and $_FILES['userfile2'].

If in doubt:
Expand|Select|Wrap|Line Numbers
  1. print_r($_FILES);
Alternatively, if you want to allow for the possibility of more than 2 files, you can name your inputs 'userfile[]', which will create $_FILES['userfile'][0], $_FILES['userfile'][1], $_FILES['userfile'][2], etc.

E.g.:
Expand|Select|Wrap|Line Numbers
  1.  <input name="userfile[]" type="file" /><br />
  2.  <input name="userfile[]" type="file" /><br />
  3.  <input name="userfile[]" type="file" /><br />
  4. .
  5. .
  6. .
  7. <input type="button" onclick="createFileInput();" value="Add Another" />
  8.  
Jul 8 '07 #2
Jankie
58
Thank you Pbmods for your readiness to offer a hand(as always :)
Yes,print_r positive
Now if you can afford a bit more kindness trespassing,which do you think is the best(or the correct) way of finishing it up this multiple uploads ?

Expand|Select|Wrap|Line Numbers
  1. foreach ($_FILES["userfile"]["error"] as $file) {
  2. if (move_uploaded_file($file['userfile']['tmp_name'][$file], $uploadfile)) {
  3. ....
  4. }
or

Expand|Select|Wrap|Line Numbers
  1. foreach ($_FILES["userfile"]["error"] as $file) {
  2. move_uploaded_file($file["userfile"]["tmp_name"],$file["userfile"]["name"],$uploadfile) 
  3. ..
  4. }
Jul 9 '07 #3
pbmods
5,821 Expert 4TB
Heya, Jankie.

What you'll want to do is iterate through $_FILES itself:
Expand|Select|Wrap|Line Numbers
  1. foreach($_FILES as $array)
Since $_FILES is going to look like this:
Expand|Select|Wrap|Line Numbers
  1. $_FILES = array(
  2.     'userfile1' => array(  //  <-- We want to work with this array first...
  3.         'name' => 'apicture.jpg',
  4.         'type' => 'image/jpeg',
  5.         'size' => '45000',
  6.         'tmp_name' => '/tmp/php_123456abcd',
  7.         'error' => 0
  8.     ),
  9.     'userfile2' => array(  //  ... then this one.
  10.         'name' => 'anotherpicture.jpg',
  11.         'type' => 'image/jpeg',
  12.         'size' => '48000',
  13.         'tmp_name' => '/tmp/php_dcba_654321',
  14.         'error' => 0
  15.     )
  16. );
  17.  
Probably what you'll want to do first is make sure that each file was uploaded correctly:
Expand|Select|Wrap|Line Numbers
  1. $errors = array();
  2. foreach($_FILES as $key => $array)
  3.     if($array['error'] !== UPLOAD_ERR_OK)
  4.         $errors[] = "$key had error {$array['error']}!";
  5.  
  6. if(! empty($errors))
  7.     exit(implode('<br />', $errors));
  8.  
Once you've determined that there were no errors, then you can process your files:
Expand|Select|Wrap|Line Numbers
  1. foreach($_FILES as $key => $array) {
  2.     if(! move_uploaded_file( $array['tmp_name'], $array['name'] ))
  3.         $errors[] = "$key could not be moved!";
  4. }
  5.  
  6. if(! empty($errors))
  7.     exit(implode('<br />', $errors));
  8.  
Jul 9 '07 #4
pbmods
5,821 Expert 4TB
Thanks for using CODE tags! Did you know that you can specify a language for your CODE tags to make your source code easier to read?

You will still need to use [/code] to close your code blocks, regardless of the language, but you can the use one of these tags to open your code block:

[code=html]
[code=javascript]
[code=php]

and so on.

Thanks!

MODERATOR
Jul 9 '07 #5
Jankie
58
Thanks a lot Pbmods
Interesting the code thing,never heard of it as a closing tag
Time to sleep for the day
Happy weekdays ahead !
Jul 9 '07 #6
pbmods
5,821 Expert 4TB
Heya, Jankie.

Interesting the code thing,never heard of it as a closing tag
Yeah, it can be a little confusing. For more info, check out the 'REPLY GUIDELINES' box on the right side of the page when you post.
Jul 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: lion | last post by:
I get these errors when uploading images via a web page: (the page still uploads the images but reports these errors?) Warning: fopen(D:\php\uploadtemp\php1FC7.tmp) : failed to create stream: No...
2
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
5
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
2
by: Jason | last post by:
Hi I have this web application for an intranet. I would like to be able to upload 1 or more files from the client machine. for example: the client is working with 10 files and after he is done...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
4
by: Corey Erkes | last post by:
I am using ASP.Net with C# as the code behind and am trying to upload multiple files with the HTMLInputFile Control. I am able to browse, select a file, and finally upload, but only one file at a...
2
by: tshad | last post by:
I have a page that I am using for multiple uses that includes reposting of the page as well as going to another page where the "form" tag would be something like: <form id="something"...
1
by: Sönke Greve | last post by:
Hi there, i want to enable uploading lots of images and later display it as an online gallery. Therefore i need a way of using the FileUpload Component with selecting multiple files or a whole...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
2
by: Zoliq | last post by:
Hi, I have a serious problem. I'm currently developing an application in which the user should upload many files (600-700 xml files). I've searched the web for a while and so far I realized that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.