流明文件上传
Lumen file upload
我有以下文件上传代码:
$picName = $request->file('cnicFrontUrl')->getClientOriginalName();
$picName = Carbon::now() . $picName;
$destinationPath = '/uploads/user_files/cnic';
$request->file('cnicFrontUrl')->move($destinationPath, $picName);
在我的 public 文件夹中有 uploads/user_files/cnic
.
我收到的异常:
{
"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
}
这里缺少什么?
"uploads/user_files/cnic17-05-22 09:06:15cars.png\"
问题是目标斜线。 windows 上的目录分隔符是 \
此外,:
不允许出现在文件名中
试试这个
$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);
不能这样设置文件名
2017-05-22 09:06:15cars.png
所以使用 uniqid() 函数获取图像的唯一文件名
使用 DIRECTORY_SEPARATOR
而不是 / 或 \,这将自动为您的平台获得独立的目录分隔符。
例子
<?php
// will output "../public/logo.png" in unix or "..\public\logo.png"
$path = ".." . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "logo.png"
echo $path;
我有以下文件上传代码:
$picName = $request->file('cnicFrontUrl')->getClientOriginalName();
$picName = Carbon::now() . $picName;
$destinationPath = '/uploads/user_files/cnic';
$request->file('cnicFrontUrl')->move($destinationPath, $picName);
在我的 public 文件夹中有 uploads/user_files/cnic
.
我收到的异常:
{
"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
}
这里缺少什么?
"uploads/user_files/cnic17-05-22 09:06:15cars.png\"
问题是目标斜线。 windows 上的目录分隔符是 \
此外,:
不允许出现在文件名中
试试这个
$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);
不能这样设置文件名
2017-05-22 09:06:15cars.png
所以使用 uniqid() 函数获取图像的唯一文件名
使用 DIRECTORY_SEPARATOR
而不是 / 或 \,这将自动为您的平台获得独立的目录分隔符。
例子
<?php
// will output "../public/logo.png" in unix or "..\public\logo.png"
$path = ".." . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "logo.png"
echo $path;