2016-03-06 57 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

相關問題