Laravel 5.1 如何检查目录中是否存在文件?

Laravel 5.1 How to check for the presence a file in a directory?

我有一个表单,用户可以在其中将文件上传到上传文件夹。在控制器中,我需要检查文件是否已存在于上传目录中,如果存在则不允许将同一文件上传两次。 我该如何检查?

如果您的文件存储在 "public" 文件夹中,请尝试以下操作:

\File::exists(public_path().'\uploads\'.$file->getClientOriginalName())

也许问题在于您正在检查文件是否存在于服务器的根文件夹中。

试试这个

    if (file_exists(public_path('uploads/'.$file->getClientOriginalName()))) {
        return redirect()->back()->withInput()->withErrors([' File already exists.']);
    }

首先,您必须在控制器

的页面顶部提到使用文件
$imagepath = "1.jpg";

$directoryPath = 'user_img/'.$imagepath;

if(File::exists($directoryPath)){
    //echo "n exist"; die();
} else {  echo "not exists";   }