如何保存文件从$请求在laravel?



我在控制器中有一个函数

public function upload(Request $request)
{
$file = $request->file('File');
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());

}

如果我在日志中发送$request它会显示如下内容

array (
'_id' => 'tuYDOc644W6DDgAS',
'_token' => 'FerVRJvJWtnzv91TGuFRpIeT173aD9pH2o9Pqcu9',
'upload_column' => 'File',
'id' => 'WU_FILE_0',
'name' => '134772132_217259409961521_6013657189083751736_o.jpg',
'type' => 'image/jpeg',
'lastModifiedDate' => '2/7/2021, 6:44:12 AM',
'size' => '629882',
'_file_' => 
IlluminateHttpUploadedFile::__set_state(array(
'test' => false,
'originalName' => '134772132_217259409961521_6013657189083751736_o.jpg',
'mimeType' => 'image/jpeg',
'error' => 0,
'hashName' => NULL,
)),
) 

但是这是在调用成员函数move() on null

我正在使用laravel-admin包,我如何正确保存文件和获取有关文件的信息?

我刚刚发现,它与laravel-admin包有点不同

请求对象是

array (
'_id' => 'tuYDOc644W6DDgAS',
'_token' => 'FerVRJvJWtnzv91TGuFRpIeT173aD9pH2o9Pqcu9',
'upload_column' => 'File',
'id' => 'WU_FILE_0',
'name' => '134772132_217259409961521_6013657189083751736_o.jpg',
'type' => 'image/jpeg',
'lastModifiedDate' => '2/7/2021, 6:44:12 AM',
'size' => '629882',
'_file_' => 
IlluminateHttpUploadedFile::__set_state(array(
'test' => false,
'originalName' => '134772132_217259409961521_6013657189083751736_o.jpg',
'mimeType' => 'image/jpeg',
'error' => 0,
'hashName' => NULL,
)),
) 

我可以像这样获取文件名和文件并存储它们

Storage::disk('public')->put($request->name, $request->_file_);

试着编辑这个

来自:

$file = $request->file('File');

:

$file = $request-> File; //Use file input tag's name attribute here.

希望对大家有帮助。

您可以使用laravel存储:https://laravel.com/docs/8.x/filesystem#the-public-disk

只需要3个基本步骤

  1. 创建符号链接
  2. 在配置目录下配置文件系统:public_path('storage') =>storage_path (app/公共)
  3. 保存文件:存储::磁盘(公共)→把("file_name"、"文件");

您可以使用以下步骤:

  1. 用您的有效mime验证请求的文件:
$this->validate($request, [
'File' => ['required', 'mimes:jpeg,gif,bmp,png', 'max:2048']
]);
  1. 从请求中获取文件
$image = $request->file('File');
  1. 重命名给定文件名
// get the original file name and replace any spaces with _
// For example, Business Cards.png = timestamp()_business_cards.png
$filename = time()."_". preg_replace('/s+/', '_', strtolower($image->getClientOriginalName()));
  1. 将图像移动到位置(public)
$tmp = $image->storeAs('uploads', $filename, 'public');

相关内容

  • 没有找到相关文章

最新更新