关于php多文件上传的快速建议(Quick suggestion on php multiple file uploading)

编程入门 行业动态 更新时间:2024-10-27 03:27:51
关于php多文件上传的快速建议(Quick suggestion on php multiple file uploading)

我正在使用这个代码,这基本上帮助我获取用户在浏览器上删除的文件,然后将其发布到php并回显文件名但问题是在php中的数组,当我丢弃2个文件并调用php文件并尝试回显它给我的文件数总是5,它回显2个文件名和+ othes作为未定义索引....如果我上传5个文件它显示所有5没有问题.... plz帮助我为什么这是happing ...

这是我的jquery代码:

function handleFiles(droppedFiles) { var uploadFormData = new FormData($("#yourregularuploadformId")[0]); if(droppedFiles.length > 0) { // checks if any files were dropped for(var f = 0; f < droppedFiles.length; f++) { // for-loop for each file dropped alert(droppedFiles[f]['name']); uploadFormData.append("files[]",droppedFiles[f]); // adding every file to the form so you could upload multiple files } } // the final ajax call alert(uploadFormData); $.ajax({ url : "try.php?size="+s, // use your target type : "POST", data : uploadFormData, cache : false, contentType : false, processData : false, success : function(ret) { alert(ret); } }); return false; }

这是我的PHP代码:

if(isset($_FILES["files"])) { for ($i=0;$i<count($_FILES['files']);$i++) { echo $_FILES['files']['name'][$i]; echo "\n"; } }

I'm using thid code this basically helping me getting file which user drops on browser and then post it to php and echoing file name but the problem is with the array in php when ever i drop 2 files and call the php file and try to echo the count of files it gives me 5 always and it echos the 2 file names and + othes as undefined index.... and if i upload 5 files it show all 5 with no problem....plz help me why this is happing...

Here is my jquery code:

function handleFiles(droppedFiles) { var uploadFormData = new FormData($("#yourregularuploadformId")[0]); if(droppedFiles.length > 0) { // checks if any files were dropped for(var f = 0; f < droppedFiles.length; f++) { // for-loop for each file dropped alert(droppedFiles[f]['name']); uploadFormData.append("files[]",droppedFiles[f]); // adding every file to the form so you could upload multiple files } } // the final ajax call alert(uploadFormData); $.ajax({ url : "try.php?size="+s, // use your target type : "POST", data : uploadFormData, cache : false, contentType : false, processData : false, success : function(ret) { alert(ret); } }); return false; }

Here is my php code :

if(isset($_FILES["files"])) { for ($i=0;$i<count($_FILES['files']);$i++) { echo $_FILES['files']['name'][$i]; echo "\n"; } }

最满意答案

这不起作用。 $_FILES是一个关联数组,包含上传的文件,由字段名称索引。 每个条目都有五个元素: name , tmp_name , size , type和error 。 这些元素中的每一个都是包含与上载文件一样多的元素的数组。

因此,如果count($_FILES['files']) ,结果将始终为5.但如果你count($_FILES['files'][$xyz])其中$xyz是上述任何一个键, 将是是上传文件的数量。

所以你的代码会这样工作,例如:

if(isset($_FILES["files"])) { for ($i=0;$i<count($_FILES['files']['name']);$i++) { echo $_FILES['files']['name'][$i]; echo "\n"; } }

或者更好(为了便于阅读,如果没有别的话):

if(isset($_FILES["files"])) { $filenames=$_FILES['files']['name']; for ($i=0;$i<count($filenames);$i++) { echo $filenames[$i]; echo "\n"; } }

It doesn't work this way. $_FILES is an associative array containing the uploaded files, indexed by the field name. Each entry has exactly five elements: name,tmp_name,size,type and error. Each of these elements is an array containing as many elements as the uploaded files.

So if you count($_FILES['files']), the result will always be 5. But if you count($_FILES['files'][$xyz]) where $xyz is any of the above keys, that will be the number of uploaded files.

So your code would work like this, for example:

if(isset($_FILES["files"])) { for ($i=0;$i<count($_FILES['files']['name']);$i++) { echo $_FILES['files']['name'][$i]; echo "\n"; } }

or better yet (for readability, if nothing else):

if(isset($_FILES["files"])) { $filenames=$_FILES['files']['name']; for ($i=0;$i<count($filenames);$i++) { echo $filenames[$i]; echo "\n"; } }

更多推荐

files,php,file,文件,droppedFiles,电脑培训,计算机培训,IT培训"/> <meta name=&quo

本文发布于:2023-08-06 17:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453889.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件上传   快速   建议   php   Quick

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!