Multiple file upload
Moderator: General Moderators
Multiple file upload
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. 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
1. You're right, but a little off. Have all your input fields the same name, only make them like so: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)?
Code: Select all
<input type="file" name="file[]" /><br />
<input type="file" name="file[]" />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
}
}
}#2. It depends on the permissions of the directory. chmod(), perhaps?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
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.
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.
Yes, it's a superglobal.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
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:
I opted for
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:
Hope that makes sense.
Thank you again for all the help
Code: Select all
<input type="file" name="file[]" /><br />
<input type="file" name="file[]" />Code: Select all
<input type="file" name="file1" /><br />
<input type="file" name="file2" />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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.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); } }
Code: Select all
if (!empty($_FILES)) {
foreach ($_FILES as $upload) {
upload_file($upload['tmp_name']);
}
}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...
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...