在我的控制器中,我编写了保存文件的代码。代码是用我的 store()
方法编写的,它看起来像这样:
if ($request->hasFile('image')) {
$image = $request->file('image'); //request the file
$fileName = md5_file($image . microtime()) . '.' . $image->getClientOriginalExtension(); //use md5 for security reasons and get the extension.
$image->storeAs('', $fileName, 'public'); //store the file in the public folder disk.
$assortment->image_path = $fileName;
$assortment->save();
}
运行我编写的测试时出现以下错误:
ErrorException: md5_file(/tmp/phptdn2a80.30601700 1609791282): failed to open stream: No such file or directory
。
我知道这与我的控制器有关,而不是我的测试。有谁知道我该如何解决它。我尝试不使用 md5_file()
这样做:
$fileName = uniqid(microtime()) . ".{$image->getClientOriginalExtension()}";
。
这没有用。我该如何解决这个问题?
答案 0 :(得分:3)
md5_file()
计算给定文件的 md5 哈希值。您提供了错误的文件路径,即 /tmp/phptdn2a80.30601700 1609791282
。
也许你打算写这样的东西:
$fileName = md5_file($image) . microtime(). '.' . $image->getClientOriginalExtension();
关于microtime()
的附注:
在文件路径中包含多个点 (.) 和空格可能不是一个好主意。查看以下说明,谨慎使用 microtime。
microtime()
有两种用法:
microtime(false)
返回一个字符串,如 0.91979600 1609794139
microtime(true)
返回一个浮点数,如 1609794139.9197
默认的 microtime()
用法类似于 microtime(false)
,它返回一个类似 0.91979600 1609794139
的字符串
因此,您创建的文件名类似于 /tmp/filename.ext0.91979600 1609794139
希望这能解决您的问题。