![](/img/trans.png)
[英]How can I tell the PHP to save a file in a specific folder when using $_POST to attribute the filename?
[英]How can I tell if a file is text using PHP?
我正在為我們巨大的PHP代碼庫創建一個搜索引擎。
給定文件路徑,如何確定某個文件是文本文件還是某種其他類型? 我寧願不必訴諸文件擴展名(如substr($filename, -3)
或傻事),因為這是一個基於linux的文件系統,所以任何事情都與文件擴展有關。
我正在使用RecursiveDirectoryIterator,所以我也有這些方法可用..
嘗試使用finfo_file()
函數。
這是一篇描述其用法的博客: 使用PHP進行智能文件類型檢測
if (mime_content_type($path) == "text/plain") {
echo "I'm a text file";
}
您可以調用file
實用程序:
echo `file '$file'`;
返回如下內容:
$ file test.out
test.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
$ file test.cpp
test.cpp: ASCII C program text
$ file test.txt
test.txt: ASCII text
$ file test.php
test.php: PHP script text
嘗試使用: string mime_content_type ( string $filename )
希望這是有幫助的。
威廉崔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.