Page 1 of 1
Multiple file upload
Posted: Mon Oct 22, 2007 4:38 am
by ranger3
I have a form that includes text input fields and two fields for uploading files. The plan is to upload the files to the images directory that is served by my Apache2 setup. At the moment it is not working (error: no file uploaded), so I have a number of questions:
1. Is it possible to upload multiple files from one form using php (I assumed it was, referenced as $_FILES['file1'], $_FILES['file2'] etc)?
2. Should I be able to upload to the images directory or are there going to be problems with permissions (path to images directory: /home/vsftpd/nameofwebsite/www/images - with DocumentRoot in apache2 set to /home/vsftpd/nameofsite/www/)?
3. If no file is entered for upload in the form, can I prevent a call to my upload function and only process the $_POST data (for some reason using '!isset' is having no effect)?
Re: Multiple file upload
Posted: Mon Oct 22, 2007 4:43 am
by s.dot
ranger3 wrote:I have a form that includes text input fields and two fields for uploading files. The plan is to upload the files to the images directory that is served by my Apache2 setup. At the moment it is not working (error: no file uploaded), so I have a number of questions:
1. Is it possible to upload multiple files from one form using php (I assumed it was, referenced as $_FILES['file1'], $_FILES['file2'] etc)?
2. Should I be able to upload to the images directory or are there going to be problems with permissions (path to images directory: /home/vsftpd/nameofwebsite/www/images - with DocumentRoot in apache2 set to /home/vsftpd/nameofsite/www/)?
3. If no file is entered for upload in the form, can I prevent a call to my upload function and only process the $_POST data (for some reason using '!isset' is having no effect)?
1. You're right, but a little off. Have all your input fields the same name, only make them like so:
Code: Select all
<input type="file" name="file[]" /><br />
<input type="file" name="file[]" />
Notice they will all have the same name (file) followed by []. This will automatically put them into an array for PHP.
To reference them in your script (which also answers #3):
Code: Select all
if (!empty($_FILES))
{
for ($i = 0; $i<count($_FILES); $i++)
{
if (!empty($_FILES['file']['tmp_name'][$i]))
{
//process file
}
}
}
My coding may be a little off.. it's been a while since I've done multiple file uploads. =/
#2. It depends on the permissions of the directory.
chmod(), perhaps?
Posted: Mon Oct 22, 2007 6:52 am
by ranger3
Thank you for the quick response - I will try out the suggestions this evening.
There is just one more query...I have written the php upload script as a function that gets called if there is a file to upload - do I have to pass the $_FILES array to the function that handles the upload, or is the $_FILES array already accesssible from the function because of it's superglobal status? I have been working on the assumption that the latter case applies.
Thanks again.
Posted: Mon Oct 22, 2007 10:39 am
by s.dot
Yes, it's a superglobal.
Posted: Tue Oct 23, 2007 5:19 pm
by ranger3
Tried out the suggestions and, after a chmod on the destination directory, succeeded in uploading two files from one form. I did, however, make a change to the form and the upload script so that I was able to identify which file was which and rename them appropriately. So, in the html form, instead of using:
Code: Select all
<input type="file" name="file[]" /><br />
<input type="file" name="file[]" />
I opted for
Code: Select all
<input type="file" name="file1" /><br />
<input type="file" name="file2" />
This was so that I could identify which file was uploaded if only one of the files was uploaded. In the first example, if only one of the files is uploaded it will be identified as file[0], regardless of whether it is file1 or file2 (I think...). I need to identify which file is which for renaming purposes (something I think I neglected to mention in my first post).
The script that calls the upload function looks like the following:
Code: Select all
if(!empty($_FILES))
{
if(!empty($_FILES['file1']['tmp_name']))
{
upload_file($filename1);
}
if(!empty($_FILES['file2']['tmp_name']))
{
upload_file($filename2);
}
}
Hope that makes sense.
Thank you again for all the help
Posted: Tue Oct 23, 2007 11:29 pm
by John Cartwright
ranger3 wrote:
Code: Select all
if(!empty($_FILES))
{
if(!empty($_FILES['file1']['tmp_name']))
{
upload_file($filename1);
}
if(!empty($_FILES['file2']['tmp_name']))
{
upload_file($filename2);
}
}
This isn't unnecessary, and should be avoided. Using scottayy's suggestion, you use an array so you can put however many upload inputs as you want without having to manually have to add php code each time.
Code: Select all
if (!empty($_FILES)) {
foreach ($_FILES as $upload) {
upload_file($upload['tmp_name']);
}
}
Posted: Wed Oct 24, 2007 2:14 pm
by ranger3
Using an array is fine if you want to treat each uploaded file in the same manner. However, there are only ever going to be two files in the form for upload and the two files each serve different purposes once uploaded (they are gif files for use as an icon and a banner). On upload, the first file gets renamed so it can be referenced as the icon for display on a web page. The second file gets renamed so that it can be referenced as the banner. There are four possible choices to the user filling in the form and I need to be able to deal with all four:
1. The user uploads file1 (icon)
2. The user uploads file2 (banner)
3. The user uploads both file1 and file2 (icon and banner)
4. The user does not upload any of the files
I may have got this wrong but my understanding is, if I use an array, I will not know which file had been uploaded if just one file is uploaded. For example, if the user decides to upload the icon only, it will appear as file[0]. If the user decides to upload the banner only, it too will appear as file[0]. From this, it is not possible to identify in the code whether the file should be renamed as the icon or the banner.
The simplest way round this seems to be the way I described in my previous post. I avoid repeating all the code for uploading and renaming the file(s) by passing parameters to a generic upload function.
Hope that all makes sense...