如何通过单个功能上传多个图像

时间:2016-08-31 14:34:31

标签: php

这是我的HTML代码

Photo 1 : <input type="file"  name="utrpt[]" required="required">
Photo 2 : <input type="file"  name="utrpt[]" required="required">
Photo 3 : <input type="file"  name="utrpt[]" required="required">
Photo 4 : <input type="file"  name="utrpt[]" required="required">
Photo 5 : <input type="file"  name="utrpt[]" required="required">
Photo 6 : <input type="file"  name="utrpt[]" required="required">

我想将所有图像插入并上传到单个功能中。

$upload_dir = 'my_folder/'; // upload directory

$imgFile = $_FILES['utrpt']['name'];
$tmp_dir = $_FILES['utrpt']['tmp_name'];
$imgSize = $_FILES['utrpt']['size'];
$i=0;
foreach ($imgFile as $myimages)
{
$images = $ob->imageupload($myimages,$tmp_dir[$i],$imgSize[$i],$upload_dir);
$i = $i+1;
}
$ob->insert_data('tblname',array("img1" => $images['image'],"img2" => $images['image'],"img3" => $images['image'],"img4" => $images['image'],"img5" => $images['image'],"img6" => $images['image']));

这是我的 imageupload()

public function imageupload ($imgFile,$tmp_dir,$imgSize,$upload_dir)
     {
        //My upload Code
        $userpic;//my file name
        return array('image' => $userpic, 'ermsg' => $errMSG);
     }

如何将所有图像插入并上传到单个查询中?

1 个答案:

答案 0 :(得分:0)

完成此Uploading multiple files并确保按顺序执行所有操作

确保您的表单中有enctype="multipart/form-data",可以启用文件上传并执行处理部分

if ($_FILES['utrpt']) {
$file_array = reArrayFiles($_FILES['utrpt']);

  foreach ($file_array as $value) {
      echo 'File Name: ' . $value['name'];
      //or insert image link to database and upload file
  }
}

reArrayFiles功能

function reArrayFiles(&$file_post) {

$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);

for ($i=0; $i<$file_count; $i++) {
    foreach ($file_keys as $key) {
        $file_ary[$i][$key] = $file_post[$key][$i];
    }
}

return $file_ary;
}