Advertisement
Virajsinh

JSON File Create And Data Store Using PhP

Apr 28th, 2023 (edited)
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | Source Code | 0 0
  1. <?php
  2.     // https://www.geeksforgeeks.org/how-to-append-data-in-json-file-through-html-form-using-php/
  3.     $data = $_POST;
  4.  
  5.     $log_path = ROOT_DIR.'react_logs';
  6.     $log_file = $log_path.'/logs.json';
  7.  
  8.     $this->_maybe_create_upload_path($log_path); // Create Folder 'react_log'
  9.  
  10.     if(!file_exists($log_file)){
  11.         fopen(rtrim($log_path, '/').'/'.'logs.json', 'w') or die("Unable to open file!");
  12.     }
  13.  
  14.     if(file_exists($log_file))
  15.     {
  16.         // $inp = file_get_contents($log_file);
  17.         // if(!empty($inp)){
  18.         //     $tempArray = json_decode($inp, TRUE);
  19.         //     array_push($tempArray, json_decode($data['logs'], TRUE));
  20.         //     file_put_contents($log_file, json_encode($tempArray));
  21.         // }else{
  22.         //     $logs = array();
  23.         //     $logs[] = json_decode($data['logs'], TRUE);
  24.         //     file_put_contents($log_file, json_encode($logs));
  25.         // }
  26.         $tempArray = array();
  27.         $inp = file_get_contents($log_file);
  28.         if(!empty($inp)){
  29.             $tempArray = json_decode($inp, TRUE);
  30.         }
  31.         $tempArray[] = json_decode($data['logs'], TRUE);
  32.         file_put_contents($log_file, json_encode($tempArray));
  33.  
  34.         echo 'Log Added.';
  35.     }else{
  36.         echo 'Log File Not Create in Server';
  37.     }
  38. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement