我在public/image目錄中有一些圖像文件,因此我想確定在保存新文件之前該目錄中是否存在文件。 如何確定文件是否存在?如何確定文件存在於公共目錄中Laravel 5.2
1
A
回答
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
希望它有幫助:)
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;
}
相關問題
- 1. 如何刪除公共網址(laravel 5.2)
- 2. Laravel 5.2文件上傳總是(/ tmp目錄)保存在DB
- 3. 如何統計目錄Laravel 5.2 ..中的文件數量?
- 4. Laravel 5.2視圖設置爲公共文件夾
- 5. Laravel應用:從外公共目錄
- 6. 使用ftp_ *,我如何確定公共html目錄?
- 7. 將文件保存到iOS的公共目錄中使用cordova
- 8. 如何從外部公共目錄中提供文件?
- 9. Laravel 5.2將公共文件夾更改爲共享服務器上的public_html
- 10. 如何上傳PDF文件中Laravel 5.2
- 11. 如何在Laravel中保護我公共文件夾的路徑
- 12. 如何包括葉片,其公共目錄,#Laravel
- 13. 如何在laravel(主設置)和流明(sudomain設置)中共享存儲目錄中的文件
- 14. laravel 5.2自定義登錄
- 15. Webpack將不會將我的文件捆綁在正確的公共目錄中
- 16. 如何從公共目錄重新生成md文件hexo?
- 17. Laravel公共目錄的空白頁Laravel 5
- 18. 如何在Laravel 5.2
- 19. 如何在laravel 5.2
- 20. 如何在Laravel 5.2
- 21. 如何用mod_rewrite模擬公共目錄
- 22. 如何訪問系統公共目錄
- 23. 在何處以及如何在laravel 5.2中創建從公共/存儲到存儲/應用程序/公共的符號鏈接
- 24. 如何調用公共文件夾中的圖像laravel 5.4
- 25. 關於Laravel 5.2
- 26. Laravel 5.2上傳文件無法寫入目錄
- 27. 在rails公共目錄中安裝wordpress
- 28. Laravel - 刪除公共文件夾
- 29. 如何顯示從數據庫中保存在公共圖像目錄中的圖像laravel
- 30. 如何使用JavaScript來確定目錄中是否存在文件?
謝謝!你的答案解決了我的問題 –