dqef7931 2017-04-17 09:03
浏览 20
已采纳

如何迭代上传的图像文件

I want to use foreach() to move image files into a folder and print imageupload name, tmp_name and so on. I am working on zend.

Input array:

 Array
(
    [imageUpload] => Array
        (
            [name] => P_20170224_134956_BF.jpg
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\phpBCB0.tmp
            [error] => 0
            [size] => 1057039
        )

    [imageUpload1] => Array
        (
            [name] => P_20170308_135054_BF.jpg
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\phpBCD0.tmp
            [error] => 0
            [size] => 1365444
        )

    [imageUpload2] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

Code:

$data = array_merge_recursive(
    $request->getPost()->toArray(),
    $request->getFiles()->toArray()
);
// echo'<pre>';print_r($data); 

$array = array_slice($data, 2);  
print_r($array); 
foreach($array as $files)
{ 
    foreach ($files as $file) {
        print_r($files); 
        $destination  = "public/img/".$file['name']; 
        $file_name    = $file['tmp_name'];
        move_uploaded_file($file_name,$destination);
        $data = __DIR__.('/../../../../../public/img/').$file['name'];
    }
}

print_r() with error:

 Array
    (
        [name] => P_20170224_134956_BF.jpg
        [type] => image/jpeg
        [tmp_name] => C:\xampp\tmp\phpBCB0.tmp
        [error] => 0
        [size] => 1057039
    )
    <br />
    <b>Warning</b>:  Illegal string offset 'name' in <b>C:\xampp\htdocs\3Dklik\module\Photos\src\Photos\Controller\PhotosController.php</b> on line <b>84</b><br />
    <br />
    <b>Warning</b>:  Illegal string offset 'tmp_name' in <b>C:\xampp\htdocs\3Dklik\module\Photos\src\Photos\Controller\PhotosController.php</b> on line <b>85</b><br />
    <br />
    <b>Warning</b>:  Illegal string offset 'name' in <b>C:\xampp\htdocs\3Dklik\module\Photos\src\Photos\Controller\PhotosController.php</b> on line <b>87</b><br />
    Array
    (
        [name] => P_20170224_134956_BF.jpg
        [type] => image/jpeg
        [tmp_name] => C:\xampp\tmp\phpBCB0.tmp
        [error] => 0
        [size] => 1057039
    )
  • 写回答

1条回答 默认 最新

  • dongzhuohan7085 2017-04-17 09:09
    关注

    You need not have two foreach for this scenario. Your $files is an associative array:

    foreach($array as $files)
    { 
        $destination  = "public/img/".$files['name']; 
        $file_name    = $files['tmp_name'];
        move_uploaded_file($file_name,$destination);
        $data = __DIR__.('/../../../../../public/img/').$files['name'];
    }
    

    If you use foreach for $files, then it will still dig into your inner associative array, where you cannot use key.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集