Determining If a File Exists in Laravel 5

Determining If a File Exists in Laravel 5

Solution

      @if(file_exists( public_path().'/images/photos/account/'.Auth::user()->account_id.'.png' ))
<img src="/images/photos/account/{{Auth::user()->account_id}}.png" alt="Sample Image">
@else
<img src="/images/photos/account/default.png" alt="Sample Image">
@endif

How to check if a file exists in Laravel 5.2

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'))

Laravel 5.6:Check if a file exists

You should check if file exists

@if(is_file('/path/to/file.ext'))
// code
@else
// code
@endif

You could use laravel helpers, something like:

@if(is_file(public_path('img/uploads/avatars/' . $user->Uname)))
//<img src="{{ asset('img/uploads/avatars' . $user->Uname) }}">

How to determine file exist in public directory for Laravel 5.2

You could use Laravel's storage Facade as El_Matella suggested. However, you could also do this pretty easily with "vanilla" PHP, using PHP's built-in is_file() function :

if (is_file('/path/to/foo.txt')) {
/* The path '/path/to/foo.txt' exists and is a file */
} else {
/* The path '/path/to/foo.txt' does not exist or is not a file */
}

Check if file exists with Laravel 5.5 Storage? Storage for Avatar

For future readers:

                   //e.g. user/hashMD5.jpg
$filename = $avatar->hashName('user');

$image = Image::make($avatar)->orientate()->fit(220);

$location = Storage::disk('images')->put($filename, (string) $image->encode());

if($location){
$user = Auth::user();

$oldfilename = $user->avatar;

$oldfileexists = Storage::disk('images')->exists( $oldfilename );

//Delete old avatar
if($oldfilename != 'user/profile.jpg' and $oldfileexists){
Storage::disk('images')->delete( $oldfilename );
}

//Save current image to database. Sollte ich nicht update benutzen?
$user->avatar = $filename;
$user->update();
}

With the filesystem.php:

        'images' => [
'driver' => 'local',
'root' => storage_path('app/public/images'),
'visibility' => 'public',
],

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

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 not working in Laravel 5.4

Fixed with:

public function cover()
{
if(file_exists(public_path('/upload/cover/'.$this->immagine))) {
return '/upload/cover/'.$this->immagine;
} else {
return '/upload/cover/no-cover.png';
}
}

How to check file is uploaded or not in laravel

You can check if your file variable exists as

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

$filePath = $request->ImageUpload->storeAs('DIRECTORY_IN_STORAGE', 'CUSTOM_FILE_NAME'); // it return the path at which the file is now saved


Related Topics



Leave a reply



Submit