Shyam Agarwal
Shyam Agarwal

Reputation: 119

How to check if a file exists in Laravel 5.2

I have tried the following method to check if file exist:

@if(file_exists(public_path('/user_img/'.Auth::user()->id.'.jpg')))

It returns false even when the file exists!

What is wrong with it?

Upvotes: 2

Views: 21674

Answers (2)

Chamandeep
Chamandeep

Reputation: 116

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

$imagepath = "1.jpg";

$directoryPath = 'user_img/'.$imagepath;

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

Upvotes: 1

Shyam Agarwal
Shyam Agarwal

Reputation: 119

I have corrected by adding curly braces to the User id and it works fine:

@if(file_exists(public_path().'user_img/{{ Auth::User()->id }}.jpg'))

Upvotes: 3

Related Questions