2016-03-06 44 views

回答

2

您可以使用Laravel的存储Facade作为El_Matella建议。但是,你也可以用"vanilla" PHP这样做很容易地使用PHP的内置功能is_file()

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 */ 
} 
3

您可以使用存储门面:如果您使用的是磁盘image如上图所示,你需要在你config/filesystems.php定义一个新的磁盘,并在您的disks阵列添加以下条目

Storage::disk('image')->exists('file.jpg'); // bool 

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

这里是文档,如果你想了解更多关于该门面: https://laravel.com/docs/5.2/filesystem

希望它有帮助:)

+0

谢谢!你的答案解决了我的问题 –

1

你可以使用这个小工具来检查,如果该目录是空的。

if($this->is_dir_empty(public_path() ."/image")){ 
    \Log::info("Is empty"); 
}else{ 
    \Log::info("It is not empty"); 
} 

public function is_dir_empty($dir) { 
    if (!is_readable($dir)) return NULL; 
    $handle = opendir($dir); 
    while (false !== ($entry = readdir($handle))) { 
    if ($entry != "." && $entry != "..") { 
    return FALSE; 
    } 
    } 
    return TRUE; 
} 

source

相关问题