Laravel check if storage exists is not working - php

I'm trying to check if file already exist in the storage by
if(Storage::exists($request->file_name)):
dd(var_dump("it did exist"));
else:
dd(var_dump("it did not exist"));
endif
but it always return a string "it did exist" even though the file is actually deleted or the file did not exist, any ideas, help?
I'm sure there's no problem on my storage location set up as I'm having no problem when uploading a file to the storage,
Storage::put($file_name, File::get($file));
PS: I'm on Laravel 5.0
UPDATE:
here's my filesystems.php from the config folder of my Laravel 5.0
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. A "local" driver, as well as a variety of cloud
| based drivers are available for your choosing. Just store away!
|
| Supported: "local", "s3", "rackspace"
|
*/
'default' => 'local',
/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/
'cloud' => 's3',
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path().'/employee_documents/',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
'rackspace' => [
'driver' => 'rackspace',
'username' => 'your-username',
'key' => 'your-key',
'container' => 'your-container',
'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/',
'region' => 'IAD',
],
],
];

I had the same problem on Laravel 5.5 and the solution was to use
Storage::disk('public')->exists($file_name)
instead of
Storage::exists($file_name)
On my config/filesystem.php by default I have two types of local disks (local and public) and I wasn't aware of it:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],

I really don't know the reason why it's not working in your case. I tried in my machine it's working fine.
I tried to debug what's inside Storage::exits it uses same return file_exists($path);
It might be the reason
Results of the file_exists() are cached
Try these things
php artisan config:cache
php artisan cache:clear
php artisan view:clear
chmod -R 777 storage/ or chown -R webuser:webuser storage/

This may can help you. It's working with Laravel5.4:
Storage::exists('dir/'. $filename)

if(File::exists('filePath')){
dd(var_dump("it did exist"));
}else{
dd(var_dump("it did not exist"));
}
Please try above code

Related

Laravel Nova : Avatar Fields using S3 disk but return 0 value

i try to upload avatar photo on my user model, using laravel nova Fields\Avatar applying the simpliest code, but it return 0 value on my database, the disk already set to s3 disk in filesystem also nova disk. is it something missing or i skip some of process?
oh and by the way, i already installing league/flysystem-aws-s3-v3. but not resolve it at all.
here's my code :
my nova.php config
/*
|--------------------------------------------------------------------------
| Nova Storage Disk
|--------------------------------------------------------------------------
|
| This configuration option allows you to define the default disk that
| will be used to store files using the Image, File, and other file
| related field types. You're welcome to use any configured disk.
|
*/
'storage_disk' => env('NOVA_STORAGE_DISK', 's3'),
my filesystems.php config
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DISK', 's3'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been set up for each driver as an example of the required values.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'visibility' => 'public',
'throw' => false
],
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
my resource fields nova
/**
* Get the fields displayed by the resource.
*
* #param \Laravel\Nova\Http\Requests\NovaRequest $request
* #return array
*/
public function fields(NovaRequest $request)
{
return [
Fields\Avatar::make('Avatar'),
Fields\Text::make('Name')->sortable()->rules(['required']),
Fields\Email::make('Email')->rules(['required', 'email']),
Fields\Text::make('Phone')->rules(['required']),
(new Panel('Job Position', $this->jobFields()))->limit(3),
(new Panel('Employee Information', $this->employeeFields()))->limit(2),
(new Panel('Payroll Information', $this->payrollFields()))->limit(2),
(new Panel('Date Information', $this->dateFields())),
(new Panel('Authentication', $this->authenticationFields())),
];
}
i excpected the outcome of avatar fields is returning the asset that i upload on my s3 disk, but it happen like this
the preview of my avatar
the database value on avatar column
solved guys, i just forget to set true of throw key on my filesystems.php :D
the actual error appear suddenly.
this config that i forget :
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'visibility' => 'public',
'throw' => true // <- this line has to be true for debugging
],

Storage path is not part of public path (Laravel8)

I'm using laravel 8 and I'm getting the storage error:
"Storage path
/var/www/type.adoreanime.com/htdocs/Linkati/storage/app is not part
of public path /var/www/type.adoreanime.com/htdocs/Linkati/public
(View:
/var/www/type.adoreanime.com/htdocs/Linkati/resources/views/layouts/master.blade.php)
(View:
/var/www/type.adoreanime.com/htdocs/Linkati/resources/views/layouts/master.blade.php)
(View:
/var/www/type.adoreanime.com/htdocs/Linkati/resources/views/layouts/master.blade.php)
◀"
I run nginx as my webserver so i've set /var/www/type.adoreanime.com/htdocs/Linkati/public as the root directory.
filesystems.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3", "rackspace"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
's3_public' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'visibility' => 'public',
],
],
];
Anyone know how to fix this problem?
Your directory structure might not be setup correctly. Your htdocs directory should be laravel's /public directory, which is the same directory as storage/app/public, if you linked it with storage:link. You seem to have the laravel root folder in htdocs.
Whatever is causing the error, its trying to access storage/app, not storage/app/public. the former shouldn't be visible on your server.

Check laravel websocket on server

Im trying to implement laravel websockets and trying to access dashboard.
http://127.0.0.1:8000/laravel-websockets Address leads me to the dashboard on my local machine but when i host it and try www.mysite.com/laravel-websockets, it shows that the page doesn't exist.
Im using this package for laravel websockets and AWS EC2 Instance for hosting.
broadcasting.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Broadcaster
|--------------------------------------------------------------------------
|
| This option controls the default broadcaster that will be used by the
| framework when an event needs to be broadcast. You may set this to
| any of the connections defined in the "connections" array below.
|
| Supported: "pusher", "ably", "redis", "log", "null"
|
*/
'default' => env('BROADCAST_DRIVER', 'null'),
/*
|--------------------------------------------------------------------------
| Broadcast Connections
|--------------------------------------------------------------------------
|
| Here you may define all of the broadcast connections that will be used
| to broadcast events to other systems or over websockets. Samples of
| each available type of connection are provided inside this array.
|
*/
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => false,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http'
],
],
'ably' => [
'driver' => 'ably',
'key' => env('ABLY_KEY'),
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
By default, access to the WebSocket dashboard is only allowed while your application environment is set to local.
However, you can change this behavior by overriding the Laravel Gate being used. A good place for this is the AuthServiceProvider that ships with Laravel.
public function boot()
{
$this->registerPolicies();
Gate::define('viewWebSocketsDashboard', function ($user = null) {
return in_array($user->email, [
//
]);
});
}
Refer to this doc https://beyondco.de/docs/laravel-websockets/debugging/dashboard#protecting-the-dashboard

Laravel Lumen Logging not working on production

I have a small project on Laravel/Lumen where I use logging messages like this
Log::info('User failed to login.',
['Env token' => $this->envToken, 'Request token' =>$request->header('Authorization')]);
I can check my log in storage/logs/lumen.log just fine. The problem is when I push it to my vps where the log stops working. In there I ran composer install --no-dev to install the production dependencies only.
Is there a required dev dependency for logging or it can be something else??
More info:
Vendor/laravel/lumen-framework/config/logging.php (I haven't change it, so it's by default)
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Log Channel
|--------------------------------------------------------------------------
|
| This option defines the default log channel that gets used when writing
| messages to the logs. The name specified in this option should match
| one of the channels defined in the "channels" configuration array.
|
*/
'default' => env('LOG_CHANNEL', 'stack'),
/*
|--------------------------------------------------------------------------
| Log Channels
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Drivers: "single", "daily", "slack", "syslog",
| "errorlog", "custom", "stack"
|
*/
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/lumen.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/lumen.log'),
'level' => 'debug',
'days' => 7,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Lumen Log',
'emoji' => ':boom:',
'level' => 'critical',
],
'syslog' => [
'driver' => 'syslog',
'level' => 'debug',
],
'errorlog' => [
'driver' => 'errorlog',
'level' => 'debug',
],
],
];
.env file
APP_ENV=local
APP_DEBUG=true
APP_KEY=stuffkey
APP_TIMEZONE=UTC
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=stuffdb
DB_USERNAME=stuffuser
DB_PASSWORD=stuffkey
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
RESTTOKEN=blablablabla

Storage path set using .env in Laravel 5.1

I want to configure the storage path in a Laravel 5.1 using the .env file. My bootstrap/app.php looks like this:
<?php
$app = new \Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
$app->useStoragePath(getenv('STORAGE_PATH'));
and the relevant line in .env file is:
STORAGE_PATH=/var/www/storage
This doesn't work. I figured out the Dotenv library is initialised after the bootstrap is processed so the .env variables are not available in bootstrap.php.
Is there a different place where I can set the storage path and the .env variables are available?
In config/filesystems.php you can set your storage path. Try setting your storage path there and see if it works. Note that the example below is my suggestion as to how your config/filesystems.php should look. Don't mind the s3 setup. That's a part of my project.
Remember to remove $app->useStoragePath(getenv('STORAGE_PATH')); from app.php
return [
'default' => 's3',
'cloud' => 's3',
'disks' => [
'local' => [
'driver' => 'local',
'root' => env('STORAGE_PATH'),
],
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
'rackspace' => [
'driver' => 'rackspace',
'username' => 'your-username',
'key' => 'your-key',
'container' => 'your-container',
'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/',
'region' => 'IAD',
],
],
];

Categories