laravel 流明文件上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44108963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:01:20  来源:igfitidea点击:

Lumen file upload

phplaravelapifilelumen

提问by jbe

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 ?

这里缺少什么?

回答by Dharmesh Rakholia

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() 函数作为图像的唯一文件名

回答by Roy Habeahan

use DIRECTORY_SEPARATORinstead 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 - 预定义常量

回答by jbe

"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

此外,:文件名中不允许