代码之家  ›  专栏  ›  技术社区  ›  Armen

php file\u get\u contents(“/tmp/some\u file”)无法运行php7.1

  •  4
  • Armen  · 技术社区  · 8 年前

    我在阅读 /tmp/some_file 文件

    • 我的PHP版本是 7.1.13-1+ubuntu16.04.1+deb。苏里。组织+1
    • 服务器版本:Apache/2.4.29(Ubuntu)服务器构建:2018-01-14T11:23:59

    • 文件 /tmp/some\u文件 0777 当然可以

    但是 file_exists("/tmp/some_file") file_get_contents("/tmp/some_file") 都回来了 错误

    我试过了

    $data =  file_get_contents("/tmp/some_file");
    var_dump($data); //is false
    
    // OR even this code outputs: Unable to open the file!
    $myfile = fopen("/tmp/some_file", "r") or die("Unable to open the file!");
    echo fread($myfile,filesize("/tmp/some_file"));
    fclose($myfile);
    

    以上代码都不起作用,我也没有收到任何错误消息。 当我用 www-data 终端中的用户使用 php -a 模式或直接运行 php myfile.php 上述所有函数都工作正常,文件通常会被读取。

    有没有什么有效的方法来调试它,或者它与PHP版本有某种关联?

    ---更新---

    经过一番挖掘,我发现该文件位于任何其他直接读取正常。看来问题出在 /tmp 目录,但 is_readable("/tmp") 返回true和 file\u存在(“/tmp/some\u file”) 返回值仍然为false。。。

    3 回复  |  直到 8 年前
        1
  •  3
  •   Armen    8 年前

    我在这里找到了问题答案: https://serverfault.com/questions/614781/php-script-cant-access-tmp-folder?newreg=e8651c2bcaea4a718a38f4e1dc94805b

    它与 ubuntu 16.04 apache . 对于apache服务 PrivateTmp=真 在此配置文件中 /etc/systemd/system/multi-user.target.wants/apache2.service .

    这就是为什么在通过浏览器+apache执行php文件的过程中,结果为false,而通过终端,结果为true,就像apache在不存在/tmp/some\u文件的情况下执行virtal/tmp目录一样

        2
  •  0
  •   Thamaraiselvam    8 年前

    尝试 error_get_last()

    获取上次发生的错误

    例如:

    $content = file_get_contents("/tmp/some_file");
    if($content === false){
        print_r(error_get_last());
    }
    

    $fp = fopen("/tmp/some_file", "r");
    if($fp === false){
        print_r(error_get_last());
    }
    
    echo fread($myfile,filesize("/tmp/some_file"));
    fclose($myfile);
    
        3
  •  0
  •   Benjamin    6 年前

    我尝试了“多用户.目标.想要”的解决方案,它已经工作了,但在重新启动后,PrivateTmp在某个时候又恢复了。 就像我对Apache2的主要使用是PHP一样,我最终编辑了PHP。ini和I已取消注释行sys\u temp\u dir。

    默认情况下,系统使用函数sys\u get\u temp\u dir分配的temp dir。函数sys\u get\u temp\u dir将返回“/tmp”,但事实是您的tmp文件存储在某个路径上,如/tmp/systemd-private-xxxxxxxxxxxxxxxxxxxxxxxx-apache2。服务-YYYYYY//tmp/*。所以,对我来说最有效的是:

    编辑php。ini(路径可以在PHP版本之间更改)

    sudo nano /etc/php/7.2/cli/php.ini
    

    然后取消对sys\U temp\U dir行的注释

    ; Directory where the temporary files should be placed.
    ; Defaults to the system default (see sys_get_temp_dir)
    sys_temp_dir = "/tmp"
    
    推荐文章