问题
My first post so be gentle! I've tried searching for this and there are many similar posts, but I can't seem to find anything exactly like my problem and I have been at this for hours! :-/
I'm building a system in PHP (5.3) and MySQL and part of the functionality is that a user is required to upload some files based on some previous selections. An example form is as follows:
<form action="" method="post" enctype="multipart/form-data" id="uploadFiles">
<p>
<label for="file1">Upload file 1:</label>
<input type="file" name="file1" id="image">
</p>
<p>
<label for="file2">Upload file 2:</label>
<input type="file" name="file2" id="image2">
</p>
<p>
<input type="submit" name="upload" id="upload" value="Upload">
</p>
</form>
In this instance the user is required to upload two different documents. This all works for me and I can save the 2 files to the server and then insert 2 records to a table in the database with the file name, file type (pdf, jpg) and user id etc by looping through "$_FILES" in my Upload class.
What I am trying to do however is somehow add the name attribute <input name="foo"> to the inserted row so that "File 1" would have "file1" in a column called document_type and "File 2" would have "file2". That way I can do a basic check in MySQL to see if the user has uploaded the appropriate files.
To cut to the chase, is there a way to assign or associate the INPUT "name" attribute to each file in $_FILES in my Upload class so that when when I loop through $_FILES, there is a unique document_type?
Please let me know if you require further information regarding my setup.
回答1:
foreach ($_FILES as $inputName => $uploadedFile) {
echo $inputName, ':', PHP_EOL;
print_r($uploadedFile);
}
Output
file1:
Array (
'name' => ...
'tmp_name' => ...
...
)
file2:
Array (
'name' => ...
'tmp_name' => ...
...
)
回答2:
The name of the file INPUT should be a key of the row in the $_FILES array. For example, in your example the $_FILES array should have two rows that can be referenced like so: $_FILES['file1'] and $_FILES['file2']. So when you are looping through the array just look up that key value and insert it in the row.
回答3:
Easiest not to have different names for inputs but same names written as arrays, for example name="file[]".
回答4:
I believe the question has been answered, but for future suggestion, if you need more info on the passing parameters in CI, you can use Xdebug (i use in tandem with Netbeans). I just use it for a week and it's such a great tool to have!
来源:https://stackoverflow.com/questions/13270210/upload-multiple-files-to-server-and