[英]How do I allow users to select multiple files out of a single input field using php and jquery?
I have a rental listing site where users can create a page and show photos of their listing. 我有一个出租清单网站,用户可以在其中创建页面并显示其清单的照片。 I have created a form with multiple file input fields for the users to pick the files to upload as well as a submit button:
我创建了一个带有多个文件输入字段的表单,供用户选择要上传的文件以及一个提交按钮:
<form method="post" enctype="multipart/form-data">
<input type="file" name="image_1" id="image_1" />
<input type="file" name="image_2" id="image_2" />
<input type="file" name="image_3" id="image_3" />
<input type="submit" name="submit" value="Submit" />
</form>
After the user selects their images, I am running a function which resizes and uploads them: 用户选择图像后,我将运行一个调整大小并上传图像的功能:
function uploadAndResize($imageFile, $filename) {
$folder = '../images/';
$orig_w = 400;
$filename = str_replace(' ', '_', $filename);
list($width, $height) = getimagesize($imageFile);
$src = imagecreatefromjpeg($imageFile);
$orig_h = ($height / $width) * $orig_w;
$tmp = imagecreatetruecolor($orig_w, $orig_h);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $orig_w, $orig_h, $width, $height);
imagejpeg($tmp, $folder.$filename, 100);
imagedestroy($tmp);
imagedestroy($src);
}
$imageFile_1 = $_FILES['image_1']['tmp_name'];
$filename_1 = basename( $_FILES['image_1']['name']);
$imageFile_2 = $_FILES['image_2']['tmp_name'];
$filename_2 = basename( $_FILES['image_2']['name']);
$imageFile_3 = $_FILES['image_3']['tmp_name'];
$filename_3 = basename( $_FILES['image_3']['name']);
uploadAndResize($imageFile_1,$filename_1);
uploadAndResize($imageFile_2,$filename_2);
uploadAndResize($imageFile_3,$filename_3);
The current setup I have works great to upload the photos, but I would like to replace the multiple file input fields with one single file input field that has the ability to select multiple files at one time. 我目前使用的设置非常适合上传照片,但是我想用一个文件输入字段替换多个文件输入字段,该文件输入字段可以一次选择多个文件。 I do, however, want each file that gets selected to have a name attribute of "image_" combined with the next iteration, so that it will work well with the current script I am using.
但是,我确实希望每个选择的文件都具有与下一迭代结合的name属性“ image_”,以便它与我正在使用的当前脚本很好地配合。 I also need it to call the function for as many files that were selected.
我还需要它来为选定的许多文件调用该函数。
How do I allow users to select multiple files out of a single input field using php and jquery and still have it work with my current script? 如何允许用户使用php和jquery从单个输入字段中选择多个文件,并且仍然可以在当前脚本中使用它?
using "mutiple" attribute in HTML form input elements. 在HTML表单输入元素中使用“ mutiple”属性。
for eg: 例如:
<input type="file" name="attacheDocument" multiple="multiple" />
returns array of files. 返回文件数组。
I recommend you http://valums.com/ajax-upload/ 我建议您http://valums.com/ajax-upload/
It is something difficult to make it works, but the results is awesome. 使它工作起来有些困难,但是结果却很棒。 Allow:
允许:
.- Multiple files .-多个文件
.- Progress bar 。- 进度条
This plugin contains a php script that can be easily modified. 该插件包含一个可以轻松修改的php脚本。 Just test it in the page I showed you.
只需在我向您展示的页面中进行测试即可。
Use jquery multi file upload plugin. 使用jQuery多文件上传插件。 Its simple.
这很简单。 Refer below link
参考以下链接
http://www.fyneworks.com/jquery/multiple-file-upload/ http://www.fyneworks.com/jquery/multiple-file-upload/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.