file_put_contents(result.json):无法打开流:权限被拒绝

时间:2018-03-09 13:16:26

标签: php json

我试图用PHP生成一个.JSON文件,以便在Android中使用。我收到以下错误:

  

file_put_contents(result.json):无法打开流:第18行拒绝权限。

这是我的代码:

<?php
require 'conn.php';
$name = array();//array created to store the data.
$sql="SELECT * FROM employee_data;";
$result = mysqli_query($conn,$sql)  or die ("Error in query: $query. ".mysql_error());

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $output[] = $row;
    }
}
$jsonarray = json_encode(array('output'=>$output));
echo $jsonarray;
//Take special care of the msqli and the sql queries

//creates the file
$fileName = 'result'.'.json';
if (file_put_contents($fileName, $jsonarray)) {
    echo $jsonarray." is created";
}else{
    echo "There is some error";
}
?>


谢谢你的帮助:)

1 个答案:

答案 0 :(得分:1)

<?php

    require 'conn.php';

$name = array();//array created to store the data.
$sql="SELECT * FROM employee_data;";
$result = mysqli_query($conn,$sql)  or die ("Error in query: $query. ".mysql_error());

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $output[] = $row;
    }
}
$jsonarray = json_encode(array('output'=>$output));
echo $jsonarray;
//Take special care of the msqli and the sql queries

//creates the file
$fileName = 'result.json';
if (file_put_contents($fileName, $jsonarray)) {
    echo $jsonarray." is created";
}else{
    echo "There is some error";
}
?>

问题出在您的$ fileName中。 请检查上面的代码。 我希望它有所帮助。