首页 > 解决方案 > LARAVEL - 多个文件上传不起作用和检索

问题描述

当我点击提交时,它只存储最后选择的文件,它应该存储所有图像以及如何检索这些文件?有什么建议或例子吗?我需要为多个文件设置不同的属性吗?

控制器

        if (is_array($request->carEvidence)) {
            foreach ($request->carEvidence as $key => $file) {
                $destinationPath = public_path('image/');
                $profileImage = $key . "-" . date('YmdHis') . "." . $file->getClientOriginalExtension();
                $file->move($destinationPath, $profileImage);
                $post['carEvidence'] = "$profileImage";
            }
        }

意见

   <input type="file" name="carEvidence[]" multiple>

MYSQL https://ibb.co/9sPLw8C

文件输入 https://ibb.co/VHkxRwc

标签: laravelfile-upload

解决方案


你在数组中传递多个文件,所以你必须使用循环来检索控制器中的所有文件..类似

if (is_array($request->carEvidence))
{
    foreach ($request->carEvidence as $key => $file) {
        $destinationPath = public_path('image/');
        $profileImage = $key."-".date('YmdHis') . "." . $file->getClientOriginalExtension();
        $file->move($destinationPath, $profileImage);
        // code to save in your db table
    }
}

推荐阅读