【问题标题】:file_get_contents returning false instead of is_readable truefile_get_contents 返回 false 而不是 is_readable true
【发布时间】:2014-04-24 05:32:54
【问题描述】:

我只是在使用 $_FILES 和 file_get_contents 时遇到了一些问题。

$nomFichier = $_FILES['pieceJointe']['name'];
echo is_readable($_FILES['pieceJointe']['tmp_name']);
echo $_FILES['pieceJointe']['error'];
if(file_get_contents($_FILES['pieceJointe']['tmp_name']) == false)
{
        echo "impossible de lire le fichier.<br/>";
}
else
{ 
         // store the file into a BLOB field in my database
}

is_readable 显示“1”,$_FILES['pieceJointe']['error'] 说 0。 但 file_get_contents 返回 false。

我注意到这种情况只发生在名称中包含重音符号的文件中。 仅使用字母/数字文件名一切正常。

我错过了什么吗?

ps:我是法国学生,不专业,对不起我的英语:o

谢谢!

【问题讨论】:

  • 确保您的错误报告已打开。如果 file_get_contents() 返回 false,它也应该发出一个错误,这将为您提供更多信息。
  • 我的报错出现了,error_reporting(-1);

标签: php file upload file-get-contents


【解决方案1】:

使用var_dump 查看is_readable() 的实际值。您看到的“1”实际上是$_FILES['pieceJointe']['error'] 的输出,这意味着您要上传的文件太大。见http://www.php.net/manual/en/features.file-upload.errors.php

【讨论】:

  • with var_dump : bool(true),并且我的服务器配置正确,如果文件名不包含重音符号,我可以上传文件。
【解决方案2】:

我猜你的 php.ini 文件的 file_get_contents 函数和 open_basedir 指令有冲突:

http://www.php.net/manual/en/ini.core.php#ini.open-basedir

在读取文件之前尝试使用 move_uploaded_file 函数。 它会将上传的文件从您服务器的临时目录移动到指定目录,(open_basedir 指令仅影响 move_uploaded_file 的目标参数)

【讨论】:

  • 我什至尝试这样使用它,以避免重音: move_uploaded_file($_FILES['pieceJointe']['tmp_name'], "/tmp/".$filenameWithoutAccents);但这是同样的问题。然后我使用了 file_get_contents($_FILES['pieceJointe']["/tmp/".$namewithoutaccents]);
【解决方案3】:

$_FILES['pieceJointe']['tmp_name'] 不只是一个文件名,而不是一个有效的完整路径吗? 也许在打开文件之前先用 move_uploaded_file 移动文件。

【讨论】:

  • $_FILES['pieceJointe']['tmp_name'] 包含完整路径,例如“/tmp/phpzQdskC”。
  • 但我看到 $_FILES['pieceJointe']['name'] 包含类似 fichiétèst.txt (而不是此处的 fichétèst)之类的内容,这难道不是编码问题吗?文件上传烦人吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多