简体   繁体   中英

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

I have a form in which users can upload files to the upload folder. In the controller I need to check if the file already exists in the upload directory and if it exists not to allow uploading the same file twice. How can I check this?

Maybe the problem it's that you are checking if the file exists in the root folder of the server.

Try this

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

如果您的文件存储在“公共”文件夹中,请尝试以下操作:

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

First you have to mention use File at top of the page in your controller

$imagepath = "1.jpg";

$directoryPath = 'user_img/'.$imagepath;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM