Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.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
是否不保存到json文件(php+;redis)?_Php_Linux_Redis - Fatal编程技术网

是否不保存到json文件(php+;redis)?

是否不保存到json文件(php+;redis)?,php,linux,redis,Php,Linux,Redis,为什么数据不记录在json文件中? 为什么不创建此文件? 我在Linux操作系统中编写代码 我在本地跑步 $r = new Redis(); $r->connect('127.0.0.1', 6379); $key="info_users"; $r->hmset($key, [ 'id' => 1, 'username' => 'sajjad10ss', 'password' => '1q2w3edxsz0', 'fuln

为什么数据不记录在json文件中? 为什么不创建此文件? 我在Linux操作系统中编写代码 我在本地跑步

$r = new Redis(); 
$r->connect('127.0.0.1', 6379); 

$key="info_users";

  $r->hmset($key, [
    'id' => 1,
    'username' => 'sajjad10ss',
    'password' => '1q2w3edxsz0',
    'fulname' => 'sajjad kazemi',
    'email' => 'linux@kernel.vom',
]);
  $data=array();
  $data[] = $r->hgetall($key);

$json= json_encode($data, JSON_PRETTY_PRINT);

header('Content-type: text/javascript');
if (file_put_contents('xldata.json', $json))
{
    echo "Saved json to file...";
  }
else{
    echo "Oops! Error saving json...";
  }
为什么要给出以下内容?哎呀!保存json时出错


这很可能是权限问题。您可以使用来检查这是否是问题所在

您应该将该目录发送给脚本运行所在的用户,例如,如果该目录在nginx下运行,则应发送给chown nginx:nginx xldata.json

我还为您的
文件\u put\u contents()
失败更改了一些语法

$r = new Redis(); 
$r->connect('127.0.0.1', 6379); 

$key="info_users";

  $r->hmset($key, [
    'id' => 1,
    'username' => 'sajjad10ss',
    'password' => '1q2w3edxsz0',
    'fulname' => 'sajjad kazemi',
    'email' => 'linux@kernel.vom',
]);
$data=array();
$data[] = $r->hgetall($key);

$json= json_encode($data, JSON_PRETTY_PRINT);

if (! is_writable('xldata.json') {
    die('Unable to write to xldata.json');
}
file_put_contents('xldata.json', $json) or die('Could not write to json file');

header('Content-type: text/javascript');
echo $json;

您是否尝试过
是可写的()
?使用它来检查文件是否存在,是否可以写入。是的,我使用了您提供的代码,但结果没有。结果没有什么?脚本是否因“无法写入xldata.json”而消亡?这很有趣,即使没有创建json文件,页面上也没有显示任何消息