【问题标题】:$_FILES array but only first file uploads$_FILES 数组,但只上传第一个文件
【发布时间】:2013-01-18 15:05:23
【问题描述】:

我正在尝试上传多个图像文件。如果图像名称是“icon.jpg”,那么它会被移动到与任何其他图像不同的位置 - 这似乎工作正常,但只有第一张图像被上传和移动。

脚本在第一张图片后停止并显示“查询为空”,即使我尝试上传至少 4 或 5 张其他图片。

$id = '01';
foreach($_FILES['file']['name'] as $n => $image) 
if(!empty($image)) {
{
 if ($image == 'icon.jpg'){
  $target_path = "../images/articles/".$id."_small.jpg";
  } else {
  $target_path = "../images/galleries/".$id."/pk_hb" . $id.".jpg";
  }
  move_uploaded_file($_FILES['file']['tmp_name'][$n], $target_path);
$id++;
 }
}

我已经包含了我正在使用的动态 JS 上传表单,以防出现以下问题:

<form action="<? echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" method="post" name="add">
Image 1: <input name='file[]' type='file' accept="image/*" />
<div id='file_tools'>
    <img src='../images/top/file_add.png' id='add_file' title='Add new input'/>
    <img src='../images/top/file_del.png' id='del_file' title='Delete'/>
</div>

<script type='text/javascript'>
$(document).ready(function(){
var counter = 2;
$('#del_file').hide();
$('img#add_file').click(function(){
    $('#file_tools').before('Image '+counter+': <input name="file[]" type="file" accept="image/*" />');
    $('#del_file').fadeIn(0);
counter++;
});
$('img#del_file').click(function(){
    if(counter==3){
        $('#del_file').hide();
    }   
    counter--;
    $('#f'+counter).remove();
});
});
</script>
    <input class="submit" type="submit" name="add" value="Submit" />
</form>

增加了 var_dump($_FILES) 的内容

array(1) { 
["file"]=> array(5) 
{ 
 ["name"]=> array(1) { [0]=> string(11) "cooley1.jpg" } 
 ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } 
 ["tmp_name"]=> array(1) { [0]=> string(18) "/var/tmp/phpLYaj7U" } 
 ["error"]=> array(1) { [0]=> int(0) } 
 ["size"]=> array(1) { [0]=> int(106241) } 
} 
} 

【问题讨论】:

  • NITPICK:使用标签元素。
  • 你在$_FILES上做过var_dump()吗?
  • 错误:第一个文件将是01.jpg,然后是2.jpg、3.jpg,等等...您需要使用sprintf('%02d', $id) 来进行零填充。你一直在检查移动命令的返回值吗?您是否检查了每张图片的 $_FILES 中的 ['error'] 参数以确保它确实上传了?
  • 我添加了 var_dump($_FILES) 的内容 - 尝试上传 5 张图片,但数组中只有 1 张

标签: php javascript file-upload image-uploading


【解决方案1】:

更新

与$_POST 不同,您无法通过将所有input 中的name 设置为file[] 来将文件数组发送到$_FILES。 $_FILES 自动包含您在数组中上传的所有文件 :),其中键是文件 input 元素的 name。

您必须将每个文件的名称 input 更改为 file_1, file_2, ... file_n 以使脚本了解您有多个要上传的文件。

您还必须检查max-file-uploads 中的php.ini 是否具有有效值http://php.net/manual/en/ini.core.php#ini.max-file-uploads。

【讨论】:

  • 好的,我想我明白了。我已经编辑了 JS,以便每个输入都是 file_1、file_2... 等,但是如果现在是 file_1、file_2 等,我应该将 file 属性更改为 foreach($_FILES['file']['name'] 吗?
  • 您更改为foreach($_FILES as $fieldName =&gt; $obj) $fieldName 将包含您的input 元素的名称(file_1,file_2,...),obj 将是一个数组name, type, tmp_name ...
  • 我已经按照你所说的更改了foreach,但在第一张图片之后我仍然得到Query was empty。 var_dump:array(1) { ["file_1"]=&gt; array(5) { ["name"]=&gt; string(8) "icon.jpg" ["type"]=&gt; string(10) "image/jpeg" ["tmp_name"]=&gt; string(18) "/var/tmp/phpLYaj7U" ["error"]=&gt; int(0) ["size"]=&gt; int(12296) } }`
  • 我已经更新了答案,请尝试检查您的php.ini 设置。
  • max_file_uploads 是 20,upload_max_filesize 是 3M,所以那里应该没问题 - 图片都在 150Kb 以下
猜你喜欢
  • 2011-04-29
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 2018-03-28
  • 2015-10-11
  • 2015-02-04
相关资源
最近更新 更多