简体   繁体   English

如何允许用户使用php和jquery从单个输入字段中选择多个文件?

[英]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. 返回文件数组。

Refer to this great tutorial showing how to use a multi-file jquery plugin with php. 请参阅出色的教程, 教程显示了如何在php中使用多文件jquery插件。 Note that you will have to loop through the files in your php code (refer to the tutorial mentioned above.) 请注意,您将必须遍历php代码中的文件(请参阅上述教程。)

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.

相关问题 如何允许 iOS 用户通过我网站上的 html 输入字段上传文档? - How do I allow iOS users to upload a document via an html input field on my website? 使用PHP如何从数据库表中检索某个字段值,然后将该单个字段值输入到新表中 - Using PHP how do I retrieve a certain field value from a database table and then input that single field value into a new table 我如何使用jQuery克隆多个HTML输入字段 - how do i clone a multiple html input field with jquery 如何在单个shell行中执行多个PHP文件? - How do I execute multiple PHP files in single shell line? 如何在PHP中保存多个选择字段? - How Do I Save a Multiple Select Field in PHP? 如何轻松允许用户上传多个文件? - How can I easily allow users to upload multiple files? 如何允许用户使用 php html css js 在 wyswg 中自定义电子商务商店主题 - How do I allow users to customize e commerce store themes in wyswg using php html css js JQuery / PHP一次只有一个输入字段的多个文件 - JQuery/PHP multiple files one at a time with only one input field PHP如何允许用户为博客设计自定义html? - PHP how do I allow users to customize html for their blog design? 如何允许我的用户在PHP中自定义他们的仪表板 - How do I allow my users to customize their dashboards in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM