Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何正确引用$\u文件中的索引?_Php_Arrays_Image_Upload - Fatal编程技术网

Php 如何正确引用$\u文件中的索引?

Php 如何正确引用$\u文件中的索引?,php,arrays,image,upload,Php,Arrays,Image,Upload,我有以下代码: foreach($_FILES['add_prod_image'] as &$image){ $target_file = $target_dir . basename($image["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 我得到以下错误: 注意:未定义索引:中的名称 /home/matthew/www/cart/admi

我有以下代码:

   foreach($_FILES['add_prod_image'] as &$image){ 
$target_file = $target_dir . basename($image["name"]); 
$uploadOk = 1; 
$imageFileType
    = pathinfo($target_file,PATHINFO_EXTENSION);
我得到以下错误:

注意:未定义索引:中的名称 /home/matthew/www/cart/admin/process.php,第30行

但是当我
var_dump($_文件['add_prod_image']['name'])

我得到:

array(1) {
    [0]=> string(16) "plate with 5.jpg"
} 
我试着用一张图片来测试

输出是正确的,名称、tmp_名称和我需要的所有其他内容都在那里,但只要我尝试将其存储在变量中(例如,
$image=$\u FILES['add_prod_image']
),我就开始得到索引错误


有什么想法吗?

在我的示例代码中,您缺少指定为$index的索引:

这假设“我的代码段”中的HTML与您的类似。

这适用于具有多个

foreach ($_FILES["add_prod_image"]["name"] as $index=> $image) {
  $target_file = $target_dir . basename($image["name"][$index]);
}
检索多个图像的“正确”方法是:

foreach ($_FILES["add_prod_image"]["error"] as $key => $error) {
  if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["add_prod_image"]["tmp_name"][$key];
    $target_file = $target_dir . basename($_FILES["add_prod_image"]["name"][$key]);
    move_uploaded_file($tmp_name, $target_file);
  }
}

array (
  'add_prod_image' => 
  array (
    'name' => 
    array (
      0 => 'image1.jpg',
      1 => 'image2gif',
      2 => 'image3.gif',
    ),
    'type' => 
    array (
      0 => 'image/jpeg',
      1 => 'image/gif',
      2 => 'image/gif',
    ),
    'tmp_name' => 
    array (
      0 => '/tmp/phpS3tMZx',
      1 => '/tmp/php31MQBf',
      2 => '/tmp/phpyHuUdX',
    ),
    'error' => 
    array (
      0 => 0,
      1 => 0,
      2 => 0,
    ),
    'size' => 
    array (
      0 => 535,
      1 => 506,
      2 => 507,
    ),
  ),
)
$['add_prod_image']['name'][$index]
$['add_prod_image']['type'][$index]
$['add_prod_image']['tmp_name'][$index]
$['add_prod_image']['size'][$index]
需要HTML5文档类型。

试验台

name!=tmp_名称。你确定这是正确的代码吗?是的,我试过了,对不起,我复制的错误是很多错误之一。我将对其进行编辑以更准确地反映情况。为了理解其外部结构,
var\u dump($\u FILES)
['add\u prod\u image']是一个数组,它包含一个键变量对
name=foo
,但也包含其他键变量对,因此循环每个键时,只有一个键对将匹配OK我明白这一点。我想这是我的错,我无耻地复制了上传图像的代码,从来没有想过仔细检查它。我会试试那个解决方案,然后再给你回复。所以我试过了。我将其更改为:foreach($\u FILES['add\u prod\u image']['error']as$key=>$error){$target\u file=$target\u dir.basename($\u FILES['add\u prod\u image']['name']);但现在我得到了:警告:basename()希望参数1是字符串,数组现在我明白了!所以'name'变量存储在'name'子数组中(如果你愿意的话),索引为0到5。据我所知,$\u文件[add\u prod\u image']是一个数组,包含一个名为“name”的数组,其中包含5个索引,分别包含每个文件的名称。代码看起来正确,我将进行测试。请等一分钟。它工作正常。谢谢!文件上传得非常完美。我唯一不明白的是:如果我们使用的是$\u文件['add\u prod\u image']['name']作为我们的索引,那么脚本如何能够检索其他变量,例如$_FILES['add_prod_image']['tmp_name']或…['size']??我们是否没有被锁定在$_FILES数组中的['name']数组中?