上传多个文件到服务器和 [英] Upload multiple files to server and
问题描述
我使用PHP(5.3)和MySQL构建系统,部分功能是用户需要上传一些基于以前的文件选择。一个示例表格如下:
< form action =method =postenctype =multipart / form-数据id =uploadFiles>
< p>
< label for =file1>上传档案1:< / label>
< input type =filename =file1id =image>
< / p>
< p>
< label for =file2>上传文件2:< / label>
< input type =filename =file2id =image2>
< / p>
< p>
< input type =submitname =uploadid =uploadvalue =Upload>
< / p>
< / form>
在这个例子中,用户需要上传两个不同的文档。这一切都适用于我,我可以将2个文件保存到服务器,然后通过循环$ _FILES中的文件名,文件类型(pdf,jpg)和用户标识符等将2条记录插入数据库中的表中我的上传类。
然而我试图做的是以某种方式添加名称属性< input name =foo>
,以便文件1在名为document_type的列中具有file1,并且文件2将具有file2。这样我就可以在MySQL中做一个基本的检查,看看用户是否已经上传了相应的文件。
为了切入正题,是否有办法分配或关联我的Upload类中的$ _FILES中的每个文件都具有INPUTname属性,这样当我通过$ _FILES循环时,有一个唯一的document_type?
请让我知道您是否需要关于我的设置的更多信息。
foreach($ _FILES as $ inputName => $ uploadedFile){
echo $ inputName,':',PHP_EOL;
print_r($ uploadedFile);
}
输出
file1:
数组(
'name'=> ...
'tmp_name'=> ...
...
)
file2:
数组(
'name'=> ...
'tmp_name'=> ...
...
)
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.
foreach ($_FILES as $inputName => $uploadedFile) {
echo $inputName, ':', PHP_EOL;
print_r($uploadedFile);
}
Output
file1:
Array (
'name' => ...
'tmp_name' => ...
...
)
file2:
Array (
'name' => ...
'tmp_name' => ...
...
)
这篇关于上传多个文件到服务器和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!