Multiple File Upload with Unique Input Names for Recording
Hi all,
I have a form that contains fields as follows...
Code:
<div class="controllabel">
Scope MDB File
</div>
<div id="uploadscopemdb_div">
<input type="file" title="Browse for the file to upload." name="scopemdb" class="textbox" />
<input type="button" title="Clear this file selection." value=" x " onclick="clearFileInputField('uploadscopemdb_div')" style="font-size:8pt; height:18px; padding-left:1px; padding-top:-2px;" />
</div>
<div class="controlnotes">
Select .ZIP file containing XX.mdb.
</div>
<div class="controllabel">
Globe MDB File
</div>
<div id="uploadglobemdb_div">
<input type="file" title="Browse for the file to upload." name="globemdb" class="textbox" />
<input type="button" title="Clear this file selection." value=" x " onclick="clearFileInputField('uploadglobemdb_div')" style="font-size:8pt; height:18px; padding-left:1px; padding-top:-2px;" />
</div>
<div class="controlnotes">
Select .ZIP file containing GlobeXXX.mdb.
</div>
<div class="controllabel">
Photos
</div>
<div id="uploadphotos1_div">
<input type="file" title="Browse for the file to upload." name="photos1" class="textbox" />
<input type="button" title="Clear this file selection." value=" x " onclick="clearFileInputField('uploadphotos1_div')" style="font-size:8pt; height:18px; padding-left:1px; padding-top:-2px;" />
</div>
<div id="uploadphotos2_div">
<input type="file" title="Browse for the file to upload." name="photos2" class="textbox" />
<input type="button" title="Clear this file selection." value=" x " onclick="clearFileInputField('uploadphotos2_div')" style="font-size:8pt; height:18px; padding-left:1px; padding-top:-2px;" />
</div>
<div id="uploadphotos3_div">
<input type="file" title="Browse for the file to upload." name="photos3" class="textbox" />
<input type="button" title="Clear this file selection." value=" x " onclick="clearFileInputField('uploadphotos3_div')" style="font-size:8pt; height:18px; padding-left:1px; padding-top:-2px;" />
</div>
<div class="controlnotes">
Select any .ZIP files containing photos for this update.
</div>
I have searched and read through several threads on this forum. There are a few good examples showing how to upload multiple files by reading them into a numbered array, but I could not see one that shows you how to upload multiple files where each "name=" value of each File Input is unique.
I need these names to be unique so that at the same time the file is uploaded, I can also record the URL and match it to a specific field in my MySQL database based on the file "name" that was used to upload the file.
So in short I want to be able to:
* Upload multiple files with one form.
* Store the URL for each uploaded file in a variable.
* Write data to a certain MySQL table>field IF a file was upload with the Name associated with that field.
If this has been asked and answered before, can someone point me in the right direction? Otherwise, can someone show me how to do this?
Just use name="files[]" (example) in your multi-upload form. Append [] after the name attribute value name. Upon submission this creates $_FILES['files'][x] so you can uniquely identify each upload file.
Or I guess what I'm asking is, how can I make it so that I identify each file by $_FILES['files'][string]... or using the examples.... ['files'][scopemdb].... ['files'][globemdb] as opposed to indentifying with a number, eg: ['files'][0]... ['files'][1].... ['files'][2], etc.
It is not compulsory for all files to be uploaded so I want it so that if a user goes directly to the fifth file down in my form, which happens to be a field named "userpersonalfile", I will be able to recognise that the uploaded file came from this field.
Last edited by Chris Jacks; 02-01-2010 at 07:21 PM.
...so that if a user goes directly to the fifth file down in my form, which happens to be a field named "userpersonalfile", I will be able to recognise that the uploaded file came from this field.
You can use either method, i.e. "name=unique_name" for each or "name=uploaded_arr[]" for all. Both will store uploaded files and include any empty fields so you can easily check.
Many developers like using "name=uploaded_arr[]" for all because it's easier to loop through an array than individually check each unique name, i.e. in your case something like this:
PHP Code:
while(list($key,$value) = each($_FILES[uploaded_arr][name])) { if(!empty($value)){ // this will check if any blank field is entered $filename = $value; $field_used=$key; // rest of file processing here } }
I forgot to add, how you name the fields is a personal choice. Some folks like to simply append 1,2,3 and so on at the end, i.e. "name=file1", "name=file2" and so on. Then do something like this:
PHP Code:
for ($counter=1; $counter<=3; $counter++) { $key="file".$counter; if ($_FILES[$key][name])) { $filename = $value; $field_used=$key; // rest of file processing here } }
Bookmarks