首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在php $_FILES变量中添加额外的参数

如何在php $_FILES变量中添加额外的参数
EN

Stack Overflow用户
提问于 2015-12-14 14:37:16
回答 2查看 1.1K关注 0票数 1

我有一个多图像上传页面。问题是当我更新之前上传的图像时。之前上传的图片在数据库中有自己的ID。例如,当我点击update POST时,不是5张图片中的所有图片都没有设置。

Dump => array(1) {
  ["editproductimages"] => array(5) {
    ["name"] => array(5) {
      [0] => string(0) ""
      [1] => string(11) "desktop.jpg"
      [2] => string(40) "12071419_404680519720527_756783084_n.jpg"
      [3] => string(0) ""
      [4] => string(0) ""
    }
    ["type"] => array(5) {
      [0] => string(0) ""
      [1] => string(10) "image/jpeg"
      [2] => string(10) "image/jpeg"
      [3] => string(0) ""
      [4] => string(0) ""
    }
    ["tmp_name"] => array(5) {
      [0] => string(0) ""
      [1] => string(25) "/opt/lampp/temp/phpYJDIAO"
      [2] => string(25) "/opt/lampp/temp/phpmyxoXB"
      [3] => string(0) ""
      [4] => string(0) ""
    }
    ["error"] => array(5) {
      [0] => int(4)
      [1] => int(0)
      [2] => int(0)
      [3] => int(4)
      [4] => int(4)
    }
    ["size"] => array(5) {
      [0] => int(0)
      [1] => int(55203)
      [2] => int(33773)
      [3] => int(0)
      [4] => int(0)
    }
  }
}

我想要的是在$_FILE中添加提取数组字段,以便在post中保存他们的数据库id。

["id"] => array(5) {
          [0] => int(10)
          [1] => int(11)
          [2] => int(12)
          [3] => int(14)
          [4] => int(13)
       }

这有可能吗?

EN

回答 2

Stack Overflow用户

发布于 2015-12-14 15:10:07

我不认为这是正确的方法或正确的答案,但我认为array_push可能会对您有所帮助,因为$_FILES也是一个数组。

假设这是文件数据。

$filesData = array(
        array('editproductimages'=>array(
                array("name"=>array("","desktop.jpg","12071419_404680519720527_756783084_n.jpg","","")),
                array("type"=> array("","image/jpeg","image/jpeg","","")),
                array("tmp_name"=>array("","/opt/lampp/temp/phpYJDIAO","/opt/lampp/temp/phpmyxoXB","","")),
                array("error"=>array(4,0,0,4,4)),
                array("size"=>array(0,55203,33773,0,0))
            ))
    );

下面的数组($ids)就是你想要的added.You,只需要用array_push把它加起来就行了。

    $ids = array('id'=>array(10,11,12,14,13));
    //actual insertion of $ids to the $filesData variable
    array_push($filesData[0]['editproductimages'],$ids);
    echo '<pre>';
    var_dump($filesData);
    echo '</pre>';

我希望这对你有帮助

票数 0
EN

Stack Overflow用户

发布于 2015-12-14 15:13:54

你可以这样做,假设你有这样的表单

<form action="">
    <!-- For your first image  -->
    <input type="hidden" name="image_ids[]" value="<?php ..place first image id.. ?>" />
    <input type="file" name="image[]"/>

    <!-- For your second image  -->
    <input type="hidden" name="image_ids[]" value="<?php ..place second image id.. ?>" />
    <input type="file" name="image[]"/>

    <!-- For your third image  -->
    <input type="hidden" name="image_ids[]" value="<?php ..place third image id.. ?>" />
    <input type="file" name="image[]"/>

    .....
</form>

将此表单提交给php后,您将在$_FILE['image']$_POST['image']数组中获得相同的值序列,您可以创建循环

foreach($_POST['image_ids'] as $k => $id){
    // process and save $_FILE['image'][$k] 
    // use $id to save/update necessary values in db
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34261078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档