dtbc37573 2019-07-13 18:03
浏览 285

使用FormData和PHP进行多个文件上传

I'm running into an issue where I can choose multiple files fine from a form but when I check it in PHP, it only shows one file (but in the console in my browser, its showing two files) and it isn't moving the file(s) to the new location. I'm not really sure what is going on.

Here is my code:

jQuery:

   $('#add-items-submit').on('click', function() {
     var store_name = $('#main-store-name').text();
     var item_name = $('#add-item-name').val();
     var item_price = $('#add-item-price').val();
     var item_desc = $('#add-item-desc').val();
     var item_num = $('#add-item-number').val();

     var formData = new FormData();

     formData.append('store_name', store_name);
     formData.append('item_name', item_name);
     formData.append('item_price', item_price);
     formData.append('item_desc', item_desc);
     formData.append('item_num', item_num);

     if (document.getElementById('add-item-image') == null) {
         document.getElementById('add-items-message').value = "Please add an image of the item if you wish to submit it";
     }

     $.each($('#add-item-image')[0].files, function (i, file) {
        formData.append('item_file', file);
     });

     $.ajax({
        type: "POST",
        contentType: false,
        processData: false,
        url: '/user/add-items',
        dataType: "json",
        data: formData
     }).done(function (msg) {
        console.log(msg);
        $('#add-items-message').html(msg.success);
     }).fail(function (msg) {
        console.log(msg);
        $('#add-items-message').html(msg.failure);
     });
  });

PHP Code:

 public function additemsAction()
 {
    $layout = $this->layout();
    $layout->setTerminal(true);

    $view_model =  new ViewModel();
    $view_model->setTerminal(true);

    if ($this->getRequest()->isPost()) {
        try {
            $store_name = $this->params()->fromPost('store_name');
            $item_name = $this->params()->fromPost('item_name');
            $item_price  = $this->params()->fromPost('item_price');
            $item_desc = $this->params()->fromPost('item_desc');
            $item_num = $this->params()->fromPost('item_num');
            $item_image = $this->params()->fromFiles('item_file');

            var_dump($item_image);

            if ($this->getUserService()->addItemsToStore($store_name, $item_name, $item_desc, $item_image, $item_num, $item_price)) {
                echo json_encode(array('success' => 'Item was successfully added to your store'));
            }
        } catch (\Exception $e) {
            echo json_encode(array('failure' => $e->getMessage()));
        }
    }


    return $view_model;
}

if (!is_dir(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $this->store_name . '/items')) {
    mkdir(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id .  '/' . $this->store_name . '/items', 0777, true);
}

for ($i=0; $i<count($item_image); $i++) {
   if (is_uploaded_file($item_image['tmp_name'][$i])) {
       move_uploaded_file($item_image['tmp_name'][$i], realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $this->store_name . '/items/' . $item_image['name'][$i]);

        $this->imagick = new \Imagick(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id .  '/' . $this->store_name . '/items/' . $item_image['name'][$i]);
        // automatically enhance them
        $this->imagick->enhanceImage();

        $this->imagick->writeImagesFile(fopen(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id .  '/' . $this->store_name . '/items/' . $item_image['name'][$i], 'wb'));
   }
}

So, basically its not moving the files to the new location and as such executing imagick's functions on them, but I've checked my logs and no error was written in the apache logs, so I'm really confused. Oh, and I did use var_dump and the result was "array(5) {↵ ["name"]=>↵ string(40) "80E0C7D8-CEB1-49E5-839F-A08F0DE798CB.jpg"↵ ["type"]=>↵ string(10) "image/jpeg"↵ ["tmp_name"]=>↵ string(24) "C:\xampp\tmp\php89B6.tmp"↵ ["error"]=>↵ int(0)↵ ["size"]=>↵ int(68903)↵}

So formData is getting all the files selected but PHP is only showing one, not multiple.

Any help would be appreciated.

Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题