简体   繁体   English

流明文件上传

[英]Lumen file upload

I have the following code for the file upload :我有以下文件上传代码:

$picName = $request->file('cnicFrontUrl')->getClientOriginalName();
$picName = Carbon::now() . $picName;
$destinationPath = '/uploads/user_files/cnic';
$request->file('cnicFrontUrl')->move($destinationPath, $picName);

In my public folder i have uploads/user_files/cnic .在我的公共文件夹中,我有uploads/user_files/cnic

The exception i receives :我收到的异常:

{
  "message": "Could not move the file \"D:\\xampp\\tmp\\php2D1C.tmp\" to \"uploads/user_files/cnic\\2017-05-22 09:06:15cars.png\" ()",
  "status_code": 500
}

Whats missing here ?这里缺少什么?

Try this尝试这个

        $picName = $request->file('image')->getClientOriginalName();
        $picName = uniqid() . '_' . $picName;
        $path = 'uploads' . DIRECTORY_SEPARATOR . 'user_files' . DIRECTORY_SEPARATOR . 'cnic' . DIRECTORY_SEPARATOR;
        $destinationPath = public_path($path); // upload path
        File::makeDirectory($destinationPath, 0777, true, true);
        $request->file('image')->move($destinationPath, $picName);

We can not set file name like this我们不能像这样设置文件名

2017-05-22 09:06:15cars.png 2017-05-22 09:06:15cars.png

So use uniqid() function for unique file name of image所以使用 uniqid() 函数作为图像的唯一文件名

"uploads/user_files/cnic\\2017-05-22 09:06:15cars.png\\" “上传/user_files/cnic\\2017-05-22 09:06:15cars.png\\”

The issue is the destination slashes.问题是目标斜线。 Directory separator on windows is \\ Windows 上的目录分隔符是\\

Also, : is not allowed in file name此外,文件名中不允许使用:

use DIRECTORY_SEPARATOR instead of / or \\, this will automatically get independent directory separator for your platform.使用DIRECTORY_SEPARATOR而不是 / 或 \\,这将自动为您的平台获得独立的目录分隔符。

example例子

<?php

// will output "../public/logo.png" in unix or "..\\public\\logo.png"
$path = ".." . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "logo.png"
echo $path;

Reference : php.net - Predefined Constants参考: php.net - 预定义常量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM