2013-01-07 37 views
0

我的數據庫表中有一些行在目錄上有一些不同步。PHP跳過文件擴展名區分大小寫

ex。在我的桌子我有

image.png 

,但在我的目錄我有

image.PNG 

現在而言,我有一個問題,以檢查是否

file_exist 

因爲案件的敏感性。 我無法選擇手動同步我的數據庫和我的文件的選項,因爲它太多了。

我可以知道如何使用file_exist忽略文件類型的敏感性嗎?

+0

可能重複的[PHP案例不敏感版本的文件\ _exists()](http://stackoverflow.com/questions/3964793/php-case-insensitive-version-of-file-exists) –

+0

你可以輕鬆存儲文件以小寫字母或將它們放入具有正確名稱的數據庫中。當然,你可以運行拋出目錄找到真實的名稱,但它的錯誤解決方案,如果你去扔數以千計的文件,這可以將PHP進程放入磁盤等待狀態和放棄請求。 –

回答

2

創建一個函數來檢查兩個擴展名,並直接使用它來代替file_exist。

3

對於一個更通用的解決方案,請參閱PHP文檔的評論:

General Solution

其中提供這個版本的功能:

/** 
* Alternative to file_exists() that will also return true if a file exists 
* with the same name in a different case. 
* eg. say there exists a file /path/product.class.php 
* file_exists('/path/Product.class.php') 
* => false 
* similar_file_exists('/path/Product.class.php') 
* => true 
*/ 
function similar_file_exists($filename) { 
    if (file_exists($filename)) { 
    return true; 
    } 
    $dir = dirname($filename); 
    $files = glob($dir . '/*'); 
    $lcaseFilename = strtolower($filename); 
    foreach($files as $file) { 
    if (strtolower($file) == $lcaseFilename) { 
     return true; 
    } 
    } 
    return false; 
} 
+1

謝謝!它的作品適合我 –

1

,因爲我覺得,你根本原因是由於以下 -
如果您的數據庫有「image.png」,並且在目錄「image.PNG」意味着您的插入查詢存在一些問題。
這兩個地方必須相同。由於Linux系統區分大小寫,最好建議以小寫形式存儲文件名。

相關問題