上传图像并提取文件名而不是文件路径

Upload image and extract filename instead of file path

我正在使用以下代码在 "storage/app/public/websites"

中上传缩略图
 $path = $request->file('thumbnail')->store('public/websites');

它工作正常并将图像上传到 "websites" 目录,但问题是 return 是实际路径,例如websites/r63mAKN1kil3BIwvwwRevOv93MgWQFme39BwH8ZV.jpeg

我只想保存图像名称,例如r63mAKN1kil3BIwvwwRevOv93MgWQFme39BwH8ZV.jpeg 在数据库中 table。

默认情况下 Laravel 为图像名称生成唯一 ID。有没有办法 return 只有文件名而不是路径?

您可以使用basename函数

http://php.net/manual/en/function.basename.php

$filename = basename($path);

这是哈希名称,所以,首先我认为你需要分开你的步骤。

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

$path = $file->store('public/websites');

需要添加文件名时可以使用$file->hashName();

这就是您所需要的

$extension = $request->image->getClientOriginalExtension();
$image_name = str_replace(' ', '', trim($request->model) . time() . "." . $extension);

并将图像移动到您想要的文件夹,请使用:

  $image_path = $request->image->move(public_path('images'), $image_name);
$info = pathinfo( $url );

$contents = ( new \GuzzleHttp\Client() )->get( $url, [ 'verify' => true ] )->getBody()->getContents();
$file = str_finish(sys_get_temp_dir(), '/') . $info[ 'basename' ];

\File::put( $file, $contents );

$ext = ( new UploadedFile( $file, $info[ 'basename' ] ) )->guessExtension();