我正在将图像文件上传到输入文件 ImageUpload 。我需要检查文件是否已上传,然后创建一个唯一的文件名并将其保存在服务器上。
$file = $request->file('ImageUpload');
$filename=uniqid($user->id . '_' ).".".$file->getClientOriginalExtension();
Storage::disk('public')->put($filename,File::get($file));
答案 0 :(得分:2)
您可以检查文件variable
是否以
if($request->hasFile('ImageUpload')){ }
但是,根据official documentation,要检查文件上传是否成功,没有任何错误,
if($request('ImageUpload')->isValid()){ }
Laravel具有广泛的用途,它使您可以保存文件,而无需将存储等写为
$filePath = $request->ImageUpload->storeAs('DIRECTORY_IN_STORAGE', 'CUSTOM_FILE_NAME'); // it return the path at which the file is now saved
答案 1 :(得分:1)
尝试这个
if($request->hasFile('ImageUpload')) { //check file is getting or not..
$file = $request->file('ImageUpload');
$filename=uniqid($user->id . '_' ).".".$file->getClientOriginalExtension(); //create unique file name...
Storage::disk('public')->put($filename,File::get($file));
if(Storage::disk('public')->exists($filename)) { // check file exists in directory or not
info("file is store successfully : ".$filename);
}else {
info("file is not found :- ".$filename);
}
}
答案 2 :(得分:1)
尝试一下。
if($request->hasFile('ImageUpload'))
{
$filenameWithExt = $request->file('ImageUpload')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('ImageUpload')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path = $request->file('ImageUpload')->storeAs('public', $fileNameToStore);
}
答案 3 :(得分:0)
我今天也遇到了同样的问题,并在Laravel Docs官方网站上找到了这个解决方案。 顺便说一下,我正在使用Laravel 5.5。
if ($request->file('imageupload')!=null)
{
return 'file uploaded';
}
else
{
return 'file not uploaded';
}