laravel 如何在laravel中检查文件是否上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53909833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check file is uploaded or not in laravel
提问by B L Praveen
I am uploading an image file to the input file ImageUpload.I need to check if file has been uploaded then create a unique filename and save it on the server.
我正在将图像文件上传到输入文件ImageUpload。我需要检查文件是否已上传,然后创建一个唯一的文件名并将其保存在服务器上。
$file = $request->file('ImageUpload');
$filename=uniqid($user->id . '_' ).".".$file->getClientOriginalExtension();
Storage::disk('public')->put($filename,File::get($file));
回答by Farooq Ahmed Khan
You can check if your file variable
exists as
您可以检查您的文件是否variable
存在
if($request->hasFile('ImageUpload')){ }
But, as per official documentation, to check whether file upload is successful without any errors,
但是,根据官方文档,要检查文件上传是否成功且没有任何错误,
if($request('ImageUpload')->isValid()){ }
Laravel is extensive, it allows you to save file without writing extra call to Storage etc. as
Laravel 是广泛的,它允许您保存文件而无需额外调用 Storage 等。
$filePath = $request->ImageUpload->storeAs('DIRECTORY_IN_STORAGE', 'CUSTOM_FILE_NAME'); // it return the path at which the file is now saved
回答by Jignesh Joisar
try this one
试试这个
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);
}
}
回答by Mik_ko
Try this.
尝试这个。
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);
}
回答by Muhammad Ali Rashed
I faced the same problem today and found this solution on official Laravel Docs. I am using Laravel 5.5 by the way.
我今天遇到了同样的问题,并在官方 Laravel Docs 上找到了这个解决方案。顺便说一下,我正在使用 Laravel 5.5。
if ($request->file('imageupload')!=null)
{
return 'file uploaded';
}
else
{
return 'file not uploaded';
}