Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php文件“获取”内容(“tmp/some”文件)无法使用php7.1_Php_Apache_Php 7.1 - Fatal编程技术网

php文件“获取”内容(“tmp/some”文件)无法使用php7.1

php文件“获取”内容(“tmp/some”文件)无法使用php7.1,php,apache,php-7.1,Php,Apache,Php 7.1,我在阅读/tmp/some_文件时遇到了一个奇怪的问题 我的PHP版本是7.1.13-1+ubuntu16.04.1+deb.sury.org+1 服务器版本:Apache/2.4.29(Ubuntu)服务器构建:2018-01-14T11:23:59 文件/tmp/some_文件肯定是0777 但是file\u存在(“/tmp/some\u file”)或file\u get\u内容(“/tmp/some\u file”)都返回false 我试过了 $data = file_get_co

我在阅读
/tmp/some_文件时遇到了一个奇怪的问题

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

  • 文件
    /tmp/some_文件
    肯定是
    0777

但是
file\u存在(“/tmp/some\u file”)
file\u get\u内容(“/tmp/some\u file”)
都返回false

我试过了

$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代码或直接运行
PHP-myfile.PHP
时,上述所有函数都工作正常,文件正常读取

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

---更新---

经过一番挖掘,我发现文件位于任何其他直接读取正常。问题似乎出在
/tmp
目录上,但
是可读的(“/tmp”)
返回true,而
文件存在(“/tmp/some文件”)
返回仍然为false

试试看

获取上次发生的错误

例如:

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


我在这里找到了问题的答案:

它与ubuntu 16.04和apache相关。对于apache服务,此配置文件
/etc/systemd/system/multi-user.target.wants/apache2.service中的PrivateTmp=true

这就是为什么在通过浏览器执行php文件的过程中,apache结果为false,通过终端为true,就像apache在不存在/tmp/some_文件的情况下生成了执行virtal/tmp目录一样,我尝试了“multi-user.target.wants”解决方案,但在重新启动后,PrivateTmp返回true。 就像Apache2的主要用途是PHP一样,我最终编辑了PHP.ini,并取消了sys_temp_dir行的注释

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

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

然后取消注释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"

PHP函数不会抛出或抛出其任何子函数。此类是用户定义异常的基类。好的,谢谢您的提示@axiaci get
无法打开流:没有这样的文件或目录
。Evan file_exists return false,因此php不知何故无法定位该文件我在想它是否与某种包含路径或类似的东西有关?您确定该file_存在吗?100%存在于终端
cat/tmp/some_file
中返回
test
甚至在
php-a
中执行的php代码上面也返回阳性结果。只有在浏览器运行失败的情况下。可能和Apache有关,我还在挖
sudo nano /etc/php/7.2/cli/php.ini
; Directory where the temporary files should be placed.
; Defaults to the system default (see sys_get_temp_dir)
sys_temp_dir = "/tmp"