問題描述
file_exists()
和 file_get_contents()
在名為 output??í?¥.txt
的文件上失敗(例如),雖然我知道它存在?
file_exists()
and file_get_contents()
fail on a file which is named output??í?¥.txt
(as an example), although I know it exists?
我猜它與文件名中的特殊字符有關嗎?.
I'm guessing its got something to do with the special characters within the file name?.
所以想知道有什么解決方法嗎?
So was wondering whats a workaround?
感謝所有回復.
僅供參考:
請注意,如果您想為什么不簡單地更改文件名?- 我不能因為文件名是一般生成的,并且更改文件名將意味著使用 PHP 的文件函數(這似乎不允許文件名 args 中的特殊字符 - 除非我誤解/誤解了任何東西).
Please note, if your thinking why not simply change the file name? - I can't as the file name is generated generically, and to change the file name will be mean using PHP's file functions (which don't seem to allow special characters within the file name args - unless I've misinterpreted/misunderstood anything).
我在 Windows 上使用 PHP 5.2.
I'm using PHP 5.2 on Windows.
推薦答案
確保文件系統的編碼與 PHP 代碼中包含文件名的字符串的編碼相同.
Ensure that the encoding of the file-system is the same as the encoding of the string that contains the file-name in your PHP code.
否則您正在測試是否存在錯誤的文件.
Otherwise you're testing for the wrong file to exist.
例如,如果您的文件系統不支持 UTF-8,但文件名以 UTF-8 編碼.
For example, if your file-system does not support UTF-8 but the file-name is encoded in UTF-8.
所以你評論說文件名是 UTF-8 并且你使用的是 windows.
So you have commented that the filename is in UTF-8 and you're using windows.
這正是我回答的重點.正如此處所寫:
That's exactly the point my answer was about. As written here:
Windows 用戶的壞消息:Windows 文件系統(FAT、FAT32、NTFS)不支持 UTF-8.根據文件系統和本地化,文件名以 ISO-8859-1 或 CP437/CP850/(或任何適合您的語言的方式)編碼.因此,如果您遵循本指南,則在嘗試將文件名從數據庫匹配到文件系統時可能會遇到麻煩.(雖然我從未嘗試過.)
Bad news for Windows Users: Windows filesystems (FAT, FAT32, NTFS) are not UTF-8 aware. Filenames are encoded in ISO-8859-1 or CP437/CP850/( or whatever suits your language) depending on filesystem and localisation. So you will probably get in trouble trying to match filenames from database to filesystem if you follow this guide. (Although I've never tried it.)
Windows 解決方法:現代 Windows 版本在安裝的共享上支持 UTF-8.共享您的媒體文件夾并將其安裝為網絡驅動器.
Windows workaround: Modern windows versions support UTF-8 on mounted shares. Make a share of your mediafolder and mount it as a network drive.
因此,要么進行共享,要么以實際適用于您的文件系統的編碼方式對文件名進行編碼.這不是 PHP 中的錯誤.您只是沒有檢查正確的文件名,僅此而已.
So either make a share or encode the filename in the encoding that actually works with your filesystem. This is not a bug in PHP. You're just no checking for the right file-name, that's all.
編輯 2:
假設您有 NTFS,它可能接受 UTF-16(這不是真正精確的 AFAIK,但您可以嘗試一下),您可以將 UTF-8 文件名轉換為 UTF-16名稱:
Assuming you have NTFS which might accept UTF-16 (which is not really precise AFAIK, but you can give it a try), you could convert the UTF-8 filename into an UTF-16 fi?ename:
$name = mb_convert_encoding($name, 'UTF-16', 'UTF-8');
然后嘗試 file_exists 是否有效.請記住,他在技術上可能不正確,而且您可能還有一個額外的編碼問題,即使進行了這種重新編碼,也無法正確使用.
And then try if file_exists works. Keep in mind that his might not be technically correct, and you might have an additional encoding issue which prevents you from correct use even with this re-encoding.
相關:NTFS 中的文件名存儲為什么編碼?
這篇關于file_exists() 和 file_get_contents() 在 PHP 中名為 output?ÕÍÕ¥.txt 的文件上失敗?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!