简体   繁体   English

使用PHP / Javascript进行多文件上传,无闪存

[英]Multi file upload with PHP/Javascript and no flash

I'm trying to make a webpage that allows the uploading of multiple files at the same times. 我正在尝试制作一个允许同时上传多个文件的网页。 I will limit the file extensions to the most common images like JPG, JPEG, PNG and GIF. 我将文件扩展名限制为最常见的图像,如JPG,JPEG,PNG和GIF。

I've done some research on this and everywhere I look it's flash this and flash that. 我已经对此进行了一些研究,无论在哪里,我都会看到闪光灯闪光灯。

I don't want to use flash really. 我真的不想使用闪光灯。 Especially with Flash 10, which disables the most common used method to enable multifile upload. 特别是Flash 10,它禁用了最常用的方法来启用多文件上传。

What I'm looking for is a way to keep creating more and more input fields, each with a browse button and then with one final upload button at the bottom of the form. 我正在寻找的是一种不断创建越来越多输入字段的方法,每个字段都有一个浏览按钮,然后在表单底部有一个最终上传按钮。 Creating the new input fields with a Javascript is nog big deal really. 使用Javascript创建新的输入字段真的很不错。

So I'm wondering how this works. 所以我想知道这是如何工作的。 Do I need to give all file-input fields the same name atribute so I can use 1 piece of PHP code to solve this? 我是否需要为所有文件输入字段赋予相同的名称属性,以便我可以使用1段PHP代码来解决这个问题? Or Is there some way for PHP to detect howmany files have been sumbitted and simply put the code for parsing a file inside a for-loop? 或者是否有一些方法可以让PHP检测出多少文件已被汇总,并简单地将用于解析文件的代码放在for-loop中?

You can keep adding 'file' inputs but use a name of something like 'upload[]' 您可以继续添加“文件”输入,但使用“upload []”之类的名称

<input type="file" name="upload[]">

Then in $_FILES['upload'] you will have an array of files you can loop over like 然后在$ _FILES ['upload']中你将拥有一个可以循环的文件数组

foreach ($_FILES['upload'] as $file) {
    echo $file['size'];
}

Here is the algorithm: 这是算法:

You add the new file input fields to your form. 您将新文件输入字段添加到窗体。 Each of this field MUST have a unique name. 每个字段必须具有唯一的名称。 Then, on the server side, you loop through the $_FILES array looking how many files have been uploaded and handling them. 然后,在服务器端,循环遍历$ _FILES数组,查看已上载的文件数量并处理它们。

In using JavaScript to add new upload fields, you could also have JavaScript update some "hidden" input field with the number of upload fields in the form. 在使用JavaScript添加新的上传字段时,您还可以让JavaScript更新一些“隐藏”输入字段,其中包含表单中的上传字段数。 That way, once you click Submit, that hidden value should be submitted and it will be trivial to parse out the $_FILES array for all the uploaded files. 这样,一旦你单击提交,就应该提交该隐藏值,并且解析所有上传文件的$ _FILES数组将是微不足道的。

If you have a reasonable maximum limit on the number of files, it's probably better to include that many file upload fields in the static form, then use the JavaScript to hide the superfluous ones, than to create them all dynamically. 如果你对文件数量有一个合理的最大限制,那么最好在静态表单中包含那么多文件上传字段,然后使用JavaScript隐藏多余的文件,而不是动态创建它们。 Then the form can still work when JavaScript is unavailable. 然后,当JavaScript不可用时,表单仍然可以工作。

Side note: technically, single HTML file upload forms are already multiple file upload forms. 附注:从技术上讲,单个HTML文件上传表单已经是多个文件上传表单。 According to the HTML form encoding standard, one should be able to select multiple files in the Browse dialogue box, and they'd be submitted as a multipart/mixed MIME structure. 根据HTML表单编码标准,应该能够在“浏览”对话框中选择多个文件,并将它们作为多部分/混合MIME结构提交。

However, almost nothing actually supports this. 但是,几乎没有任何实际支持这一点 Older versions of Opera do on the client-side (and, I think, an ancient test browser of some sort, possibly Viola), and a few form-parsing components on the server-side, but not the PHP built-ins. 较旧版本的Opera在客户端(并且,我认为,某种古老的测试浏览器,可能是Viola),以及服务器端的一些表单解析组件,而不是PHP内置函数。 In any case the UI is not very usable, so you're not missing much. 在任何情况下,UI都不是很有用,所以你不会错过很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM