以下是我运行环境的信息:
操作系统:Win 10 Pro
Apache版本:Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
PHP版本:7.1.9
FileInfo版本:1.0.5
WkHTMLtoPDF版本:wkhtmltox-0.12.4_msvc2015
我在管理员模式下的CMD
中运行了该命令:
C:\Program Files\wkhtmltopdf\bin>wkhtmltopdf http://google.com google.pdf
并且将以下PHP代码运行(在将google.pdf
移动到htdocs内的我的目录后):
$file_path = "google.pdf";
$file_info = new finfo(FILEINFO_MIME_TYPE);
var_dump($file_info->file($file_path));
输出结果为(成功):
C:\xampp\htdocs\htmlpdf\test.php:5:string 'application/pdf' (length=15)
如果这个方法对你不起作用,我建议尝试以下几种方法:
1)使用
mime_content_type
函数,虽然已经被标记为
弃用
。
$file_path = "google.pdf";
var_dump(mime_content_type($file_path));
2) 使用finfo_open
函数,实际上和new finfo
一样,但还是值得一试:
$file_path = "google.pdf";
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file_path);
finfo_close($finfo);
var_dump($mimetype);
3) 或者如果你不介意使用外部库和回退依赖项,可以尝试以下方法:
- http://getid3.sourceforge.net/
- https://dev59.com/THM_5IYBdhLWcg3wyWSt#3664655
'application/pdf'
。 - mega6382echo phpversion('fileinfo');
2)确保文件在$file_path
存在? - Ivan Kalita