访问使用 Plupload 上传的文件时遇到问题
我们正在使用多文件上传器插件,我们希望使用 move_uploaded_file() 将其保存到服务器,并在成功上传后发送包含所有文件直接链接的邮件。我们正在获取以下数组...
Array
(
[html5_uploader_0_tmpname] => p15rkfr6oogdt1ln91t0r4obrrh3.jpg
[html5_uploader_0_name] => show_041.jpg
[html5_uploader_0_status] => done
[html5_uploader_1_tmpname] => p15rkfr8mb1plfu4q1giu3v01jl74.jpg
[html5_uploader_1_name] => 23.jpg
[html5_uploader_1_status] => done
[html5_uploader_count] => 2
)
并且我们正在使用以下代码...
$("#html5_uploader").pluploadQueue({
// General settings
runtimes : 'html5',
url : 'upload.php',
max_file_size : '10mb',
chunk_size : '1mb',
unique_names : true,
filters : [
{title : "Image files", extensions : "jpg,gif,png"},
{title : "Zip files", extensions : "zip"}
],
// Resize images on clientside if we can
//resize : {width : 320, height : 240, quality : 90}
});
}
所以问题是我们应该在 $_FILES["file"]["tmp_name"] 和 $_FILES["file"][ 处传递什么如果是这个数组,则为“name”]。
We are using a multi file uploader plugin and we want to save it to the server using move_uploaded_file() and send mail with direct link of all files after successful upload. We are getting following array...
Array
(
[html5_uploader_0_tmpname] => p15rkfr6oogdt1ln91t0r4obrrh3.jpg
[html5_uploader_0_name] => show_041.jpg
[html5_uploader_0_status] => done
[html5_uploader_1_tmpname] => p15rkfr8mb1plfu4q1giu3v01jl74.jpg
[html5_uploader_1_name] => 23.jpg
[html5_uploader_1_status] => done
[html5_uploader_count] => 2
)
And we are using following code...
$("#html5_uploader").pluploadQueue({
// General settings
runtimes : 'html5',
url : 'upload.php',
max_file_size : '10mb',
chunk_size : '1mb',
unique_names : true,
filters : [
{title : "Image files", extensions : "jpg,gif,png"},
{title : "Zip files", extensions : "zip"}
],
// Resize images on clientside if we can
//resize : {width : 320, height : 240, quality : 90}
});
}
so the problem is what should we pass at place of $_FILES["file"]["tmp_name"] and $_FILES["file"]["name"] in case of this array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您更新帖子。
当单个表单中存在共享数组格式元素名称的多个文件上传时,
$_FILES
会变得很奇怪。 它在文件信息之后添加了另一层。想象一下可能会更好:
不是你所期望的,对吧?
现在,您的代码正在做一些完全无意义的事情。您正在循环
$_POST
,但随后正在处理$_FILES
。事情不是这样的。我不知道您在哪里获取发布的原始数组,所以我将忽略它。
假设
'file'
是多文件上传表单元素的名称,完全重建$_FILES
数组:(此代码<如果内部值不是数组,则可能会失败 - 请参阅 PHP 手册中的多个上传页面,用于修复
$_FILES
的其他选项。)那里。现在我们可以像从一开始就应该那样循环遍历
$fixed_files
:注意之前代码中的新添加内容 -
$safe_name
。数组的名称元素来自用户,不应被信任。正则表达式会删除除字母、数字、破折号、下划线和句点之外的任何内容。如果可以的话,您还应该对上传的数据进行更多验证!例如,如果您需要图像,则应该尝试验证文件是否确实是图像。如果您使用的是 PHP 5.3,请查看
finfo_file
。您可以使用它来检索文件的 MIME 类型,并确保它是您可以使用的文件类型。
Thanks for the updated post.
When there are multiple file uploads in a single form that share an array-formatted element name,
$_FILES
gets all wonky. Instead of adding another layer between the form element name and the file information, it adds another layer after the file information.It might be better to visualize it:
Not what you expected, right?
Now, your code is doing something utterly nonsensical. You're looping over
$_POST
, but then you're processing$_FILES
. Things don't work that way.I have no idea where you're getting the original array you posted, so I'm going to ignore it.
Let's pretend that
'file'
is the name of the multi-file upload form element and totally rebuild the$_FILES
array:(This code can fail if the inner value is not an array -- see the multiple upload page in the PHP manual for other options to fix
$_FILES
.)There. Now we can loop through
$fixed_files
like we should have been able to from the start:Note a new addition to your previous code --
$safe_name
. The name element of the array comes from the user and should not be trusted. The regex strips out anything other than letters, numbers, dashes, underscores and periods.You should also be doing more verification of the uploaded data, if you can! For example, if you are expecting images, you should attempt to verify that the files are indeed images. If you're using PHP 5.3, check out
finfo_file
. You can use it to retrieve the file's MIME type, and make sure it's a file type you can work with.发布后收到的输出存储在 $_POST 中,这是您显示的数组的来源。
为了更好地利用 $_POST 数组输出,您首先需要将 Form Posts 输出更改为多维数组/输出。
我这样做的方法是将 plupload .js 文件将 html 表单构建为多部分表单的位置。使用以下PHP方法PHP手册中的多上传页面 正如@Charles 提到的。
下面是我编辑的行的示例,使用“[]”括号将计数和“name”标记中的值名称括起来,将 $_POST 数据拆分为多维数组。
变成这样
:
然后您可以使用 Plupload 保存“Upload.php”中找到的临时(tmp)文件的位置以及 tmpname
$_POST['tmpname']
来移动文件。The output you receive after posting is stored in $_POST which is where the array you are displaying has come from.
In order to make better use of the $_POST Array output, you will firstly need to change the Form Posts output into a multidimensional Array/Output.
The way i did this was by changing where the plupload .js file constructs the html form into a multipart form. Using the following PHP method multiple upload page in the PHP manual as @Charles mentioned.
An example of the lines I edited below using the '[]' brackets around the count and the name of the value in the "name" tags which splits the $_POST data up into a multidimensional array.
Which turns this:
Into this:
Then you can use location of where Plupload saves the temporary (tmp) files found in 'Upload.php' along with the tmpname
$_POST['tmpname']
to move the file.