【问题标题】:PHP get mimetype not working docsPHP get mimetype not working docs
【发布时间】:2014-01-19 16:48:45
【问题描述】:

我只想上传图片,文档说使用 finfo_open。 http://www.php.net/manual/en/function.finfo-file.php

我这样做了,但我得到了这个错误:

致命错误:调用未定义的函数 finfo_open() C:\xampp\htdocs\ticket\index.php 第 83 行调用堆栈

时间记忆函数位置 1 2.2551 158752 {main}( ) ..\index.php:0

PHP:

if(isset($_FILES['file'])) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE); 
    $ext = finfo_file($finfo, $_FILES['file']['name']); << LINE 84
       if(substr($ext, 0, 5) != 'image') {
          $errors[] = 'Kon dit bestand type niet uploaden.';
       }
}

为什么这不起作用?

更新:

通过设置 extension=php_fileinfo.dll active 我现在明白了:

警告:finfo_file(download.jpg):打开流失败:没有这样的文件 或 C:\xampp\htdocs\ticket\index.php 中的目录第 84 行调用堆栈

时间记忆函数位置 1 2.2531 158448 {main}( ) ..\index.php:0 2 3.2572 180560 finfo_file ( ) ..\index.php:84

【问题讨论】:

  • finfo_open 需要 PHP >= 5.3.0
  • 我有:PHP 版本 5.4.7
  • 检查 php.ini fileinfo.so 是否处于活动状态

标签: php upload


【解决方案1】:

PHP 5.3.0 及更高版本已内置 Fileinfo,但在 Windows 上,您必须在 php.ini 中手动启用它

查看您的 php.ini 文件并检查 fileinfo.so 或 php_fileinfo.dll 是否已激活(取决于您的平台和版本)。

应该有类似的一行

extension = fileinfo.so

在窗户上

extension = fileinfo.dll

在您的 php.ini 文件中

更新后

使用$_FILES['file']['tmp_name'] 作为路径(finfo_file() 需要路径!)

if(isset($_FILES['file'])) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE); 
    $ext = finfo_file($finfo, $_FILES['file']['tmp_name']);

    if(substr($ext, 0, 5) != 'image') {
       $errors[] = 'Kon dit bestand type niet uploaden.';
    }
}

【讨论】:

  • 工作,但现在我得到这个:警告:finfo_file(download.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\ticket\index.php on line 84 调用堆栈 # 时间记忆函数位置 1 2.2531 158448 {main}( ) ..\index.php:0 2 3.2572 180560 finfo_file ( ) ..\index.php:84
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
相关资源
最近更新 更多