使用 HTML5 和 PHP 加载多个文件
我正在尝试使用 HTML5 加载多个文件。 这是我在某个网站上找到的代码。 在 PHP 代码中,它不将其识别为数组。
我做错了什么吗?有人可以告诉我一个可行的解决方案吗?
谢谢。
index.html
<form action='save.php' method='post' enctype='multipart/form-data'>
<input name="uploads" type="file" multiple="multiple" />
<input type='submit' value="Upload File">
</form>
保存.php
function GetFiles() {
$files = array();
$fdata = $_FILES["uploads"];
if (is_array($fdata["name"])) {//This is the problem
for ($i = 0; $i < count($fdata['name']); ++$i) {
$files[] = array(
'name' => $fdata['name'][$i],
'tmp_name' => $fdata['tmp_name'][$i],
);
}
} else {
$files[] = $fdata;
}
foreach ($files as $file) {
// uploaded location of file is $file['tmp_name']
// original filename of file is $file['file']
}
}
I am trying to load multiple files using HTML5.
This is my code I found on some site.
In the PHP code it doesn't recognize it as an array.
Am I doing something wrong? Can someone show me a working solution?
Thanks.
index.html
<form action='save.php' method='post' enctype='multipart/form-data'>
<input name="uploads" type="file" multiple="multiple" />
<input type='submit' value="Upload File">
</form>
save.php
function GetFiles() {
$files = array();
$fdata = $_FILES["uploads"];
if (is_array($fdata["name"])) {//This is the problem
for ($i = 0; $i < count($fdata['name']); ++$i) {
$files[] = array(
'name' => $fdata['name'][$i],
'tmp_name' => $fdata['tmp_name'][$i],
);
}
} else {
$files[] = $fdata;
}
foreach ($files as $file) {
// uploaded location of file is $file['tmp_name']
// original filename of file is $file['file']
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要创建某种名称数组:
就像您以前使用复选框时所做的那样。
You need to make some sort of array of the name:
Just like you used to do when using checkboxes.
作为数组的表单变量必须使用
[]
命名,否则 PHP 不会将其视为数组。所以:应该这样做。
form variables that are arrays must be named with a
[]
, otherwise PHP doesn't see it as an array. So:should do it.
我知道这是一个旧线程,但我使用这个小脚本将 $_FILES 数组(对我来说)令人困惑的布局转换为更易读的形式。 Name_input 是您在 HTML 中为输入对象指定的任何名称。
I know this is kind of a old thread but I use this little script to convert the (to me) confusing layout of the $_FILES array to a more readable form. Name_input is whatever name you gave to the Input object in HTML.