Laravel 保存图片时有时会生成一个以图片名称命名的文件夹
Laravel sometimes generates a folder with the name of the image when saving it
我将图像保存在 laravel 存储文件夹中,基本上我所有的代码都是这样
$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$name = Hash::make(Carbon::now()) . '.' .$ext;
$image->storeAs('public/images/tmp', $name);
像这样正确保存图像:
但有时它会将其保存在具有相同名称的文件夹中:
我怎样才能避免这种情况?
您的哈希正在生成一个 /
字符来创建一个文件夹。我建议使用 uuid 而不是 Hash:
$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$name = uniqid() . '.' .$ext;
$image->storeAs('public/images/tmp', $name);
$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$name = str_replace('/',"",Hash::make(Carbon::now())) . '.' .$ext;
$image->storeAs('public/images/tmp/', $name);
这曾经发生在我身上,如果 tmp 在末尾添加一个尾部正斜杠,并使用 str_replace 替换名称的所有正斜杠
我将图像保存在 laravel 存储文件夹中,基本上我所有的代码都是这样
$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$name = Hash::make(Carbon::now()) . '.' .$ext;
$image->storeAs('public/images/tmp', $name);
像这样正确保存图像:
但有时它会将其保存在具有相同名称的文件夹中:
我怎样才能避免这种情况?
您的哈希正在生成一个 /
字符来创建一个文件夹。我建议使用 uuid 而不是 Hash:
$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$name = uniqid() . '.' .$ext;
$image->storeAs('public/images/tmp', $name);
$image = $request->file('image'); $ext = $image->getClientOriginalExtension();
$name = str_replace('/',"",Hash::make(Carbon::now())) . '.' .$ext;
$image->storeAs('public/images/tmp/', $name);
这曾经发生在我身上,如果 tmp 在末尾添加一个尾部正斜杠,并使用 str_replace 替换名称的所有正斜杠