[英]Laravel 5.1 How to check for the presence a file in a directory?
我有一个表单,用户可以在其中将文件上传到上传文件夹。 在控制器中,我需要检查上传目录中是否已经存在该文件,以及是否存在不允许两次上传相同文件的问题。 我该如何检查?
可能是因为您正在检查文件是否在服务器的根文件夹中。
尝试这个
if (file_exists(public_path('uploads/'.$file->getClientOriginalName()))) {
return redirect()->back()->withInput()->withErrors([' File already exists.']);
}
如果您的文件存储在“公共”文件夹中,请尝试以下操作:
\File::exists(public_path().'\\uploads\\'.$file->getClientOriginalName())
首先,您必须在控制器页面顶部提到使用文件
$imagepath = "1.jpg";
$directoryPath = 'user_img/'.$imagepath;
if(File::exists($directoryPath)){
//echo "n exist"; die();
} else { echo "not exists"; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.