Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 如何在不返回NULL的情况下向现有JSON文件添加数据?_Php_Json - Fatal编程技术网

Php 如何在不返回NULL的情况下向现有JSON文件添加数据?

Php 如何在不返回NULL的情况下向现有JSON文件添加数据?,php,json,Php,Json,我在向现有JSON文件添加数据时遇到问题。第一次脚本运行时,所有这些都可以工作,但第二次当文件已经存在时,JSON文件会更新,但值为NULL 用于上载的PHP: $ds = DIRECTORY_SEPARATOR; if(isset($_FILES['files'])){ $errors= array(); foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file

我在向现有JSON文件添加数据时遇到问题。第一次脚本运行时,所有这些都可以工作,但第二次当文件已经存在时,JSON文件会更新,但值为NULL

用于上载的PHP:

$ds          = DIRECTORY_SEPARATOR;

if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 10097152){
            $errors[]='File size must be less than 10 MB';
        }

        if(empty($errors)==true){

            $dirPath = $ds .'hidden' . $ds .$_SESSION['user']['username']. date('d-m-Y-H-i-s'). $ds;
            $result = mkdir($dirPath, 0770, true);

            move_uploaded_file($file_tmp, $dirPath.$file_name);
            save_document_info_json($file);

        }else{
                print_r($errors);
        }
    }



    if(empty($error)){
        echo "Success";
    }
}
JSON保存文件

function save_document_info_json($file){

   $ds          = DIRECTORY_SEPARATOR;

   $jsonfile = UPLOADEDFILES."docinfo.json";

   if(is_file($jsonfile)){
      $jsonText = file_get_contents($jsonfile);
      $workflow = json_decode($jsonText, true);
   } else{
      $jsonText = '{"statistics": {"total": 0, "approved": 0}, "fileInfo":[]}';
      $workflow = json_decode($jsonText, true);
   }


   for ($i = 0; $i <= count($workflow["fileInfo"]); ++$i){
        $filename = $_FILES['files']['name'][$i];
        $filesize = $_FILES['files']['size'][$i];
        $filetype = $_FILES['files']['type'][$i];
    }

    $fileInfo["status"] = "pending";
    $fileInfo["submittedBy"] = $_SESSION['user']['username'];
    $fileInfo["approvedBy"] = "";

    $fileInfo["fileName"] = $filename;
    $fileInfo["location"] = $ds .'hidden' . $ds .$_SESSION['user']['username']. date('d-m-Y-H-i-s'). $ds;
    $fileInfo["fileType"] = $filetype;
    $fileInfo["size"] = $filesize;

    array_push($workflow["fileInfo"], $fileInfo);
    $total =  count($workflow["fileInfo"]);
    $workflow["statistics"]["total"] = $total;
    file_put_contents($jsonfile, json_encode($workflow));
}
函数save\u document\u info\u json($file){
$ds=目录\分隔符;
$jsonfile=UPLOADEDFILES.“docinfo.json”;
if(is_文件($jsonfile)){
$jsonText=file\u get\u contents($jsonfile);
$workflow=json_decode($jsonText,true);
}否则{
$jsonText='{“统计”:{“总计”:0,“批准”:0},“文件信息”:[]};
$workflow=json_decode($jsonText,true);
}

for($i=0;$i)您在for循环中做什么?这对我来说没有意义。您正在多次更新var$filename!?我这样做是因为我正在上载多个文件是的,我可以看到您希望这样做。但您只使用循环中的最后一个文件。