警告:chmod()[function.chmod]:没有这样的文件或目录

时间:2016-01-20 00:55:30

标签: php json

我试图给php文件一个写入JSON文件的权限,但它似乎不起作用。这是我的代码

<?php
$light = $_GET['light'];
if($light == "on") {
  $file = fopen("light.json", "w") or die("can't open file");
  fwrite($file, '{"light": "on"}');
  fclose($file);
} 
else if ($light == "off") {
  $file = fopen("light.json", "w") or die("can't open file");
  fwrite($file, '{"light": "off"}');
  fclose($file);
}

chmod(" /home/daffo/public_html/ard/light.json", 0755);

?>

2 个答案:

答案 0 :(得分:0)

您必须在第一个斜杠之前删除空格:

chmod("/home/daffo/public_html/ard/light.json", 0755);

答案 1 :(得分:0)

除了显而易见的,在第一次斜线之前删除空格:

chmod("/home/daffo/public_html/ard/light.json", 0755);

您在创建自己的json时遇到了麻烦。使用内置函数执行此操作:

fwrite($file, json_encode(array("light"=> "off")));