Laravel 检查存储是否存在不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38365276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel check if storage exists is not working
提问by Juliver Galleto
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
PS:我在 Laravel 5.0
UPDATE:
更新:
here's my filesystems.php from the config folder of my Laravel 5.0
这是我的 Laravel 5.0 配置文件夹中的 filesystems.php
<?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',
],
],
];
回答by vijaykumar
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);
我试图调试Storage::exits
它里面的东西使用相同的return file_exists($path);
It might be the reason
这可能是原因
Results of the file_exists() are cached
file_exists() 的结果被缓存
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/
- php工匠配置:缓存
- php artisan 缓存:清除
- php工匠视图:清除
- chmod -R 777 storage/ 或 chown -R webuser:webuser storage/
回答by Gautam Patadiya
This may can help you. It's working with Laravel5.4:
这可能会对您有所帮助。它与 Laravel5.4 一起使用:
Storage::exists('dir/'. $filename)
回答by gtamborero
I had the same problem on Laravel 5.5 and the solution was to use
我在 Laravel 5.5 上遇到了同样的问题,解决方案是使用
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:
默认情况下,在我的 config/filesystem.php 上,我有两种类型的本地磁盘(本地和公共),但我不知道:
'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'),
],
],
回答by Manikanta Siripella
if(File::exists('filePath')){
dd(var_dump("it did exist"));
}else{
dd(var_dump("it did not exist"));
}
Please try above code
请尝试上面的代码