【发布时间】:2018-12-10 07:08:40
【问题描述】:
LAMP 安装在我的本地电脑上,我知道字符串 xxxx 可以使用下面的 PHP 函数写入 /tmp/test。
file_put_contents("/tmp/test","test1 test2")
cat ajax_get.php
<?php
$str = "test1 test2";
ini_set('display_errors', 1);
error_reporting(E_ALL);
$str = implode(" ",$_GET);
file_put_contents("/tmp/test",$str);
print($str);
?>
为什么ajax_get.php中的file_put_contents("/tmp/test",$str);命令不起作用?
把file_put_contents替换成
$handle=fopen("/tmp/test","w");
fwrite($handle,$str);
fclose($handle);
如果我在ajax_get.php中更改以下语句,可能是目录权限问题
file_put_contents("/tmp/test",$str);
进入
file_put_contents("test",$str);
并运行上一个进程,ajax_get.php在/var/www/html/test创建文件
cat /var/www/html/test
test1 test2
显示/tmp 目录的权限。
ls -al /tmp
total 76
drwxrwxrwt 16 root root 12288 Dec 10 18:39 .
drwxr-xr-x 23 root root 4096 Dec 1 08:03 ..
.是当前目录/tmp,它的权限是777(rwxrwxrwx),
为什么PHP不能将文件写入/tmp目录?
【问题讨论】:
-
检查在路径上创建新文件所需的权限,如果权限可用,则设置文件所需的权限以在其中写入内容。
-
有错误信息吗?您尝试过什么调试?
-
将文件写入代码放入TRY CATCH块中,检查是否发生异常。
-
@ManojSingh 没有一个使用过的函数一开始就抛出异常,所以在这一点上尝试捕获任何异常都没有什么意义。
-
@ManojSingh 这里使用的函数都可以抛出异常,所以一开始就使用 try/catch 毫无意义。
标签: php file-put-contents