In my code I have to write multiple json strings to files. When I do so, the end of what's being written is cut off by a seemingly random amount of characters.
I originally tried using fwrite()
, then tested using just file_put_contents()
, but both methods of writing to file cut off the characters. That code snippet looks like this:
print_r( json_encode($verified_results) );
file_put_contents($results_file, json_encode($verified_results));
/*$fh = fopen($results_file, 'w'); // or die("can't open file");
$stringData = json_encode($verified_results) ;
fwrite($fh, $stringData);
fclose($fh);
print_r(preg_last_error());
*/
I know that the json string is intact post-write, as the print_r
returns the full string before writing it to file.
The text that's being cut off is:
108 characters
,"elapsedTime":"1 minute ago","hotel_cat":null,"visible":true,"hash_id":"aed1b4c6c515ea040c2e49d874c883a1"}]
110 characters
5","elapsedTime":"11 hours ago","hotel_cat":null,"visible":true,"hash_id":"22aa8da3d0b8ef44ec07f8521986fbac"}]
The first file is 47KB in size, and the cut off point leaves 108 characters at a random point (not due to an invalid character).
The second file is 52KB in size, and the cut off point leaves 110 characters, once again, at a random point.
What could be causing this with such a small file?
Edit:
(Full code)foreach ($sift_usernames as $key => $user) {
$username = $user['username'];
if(is_dir(APP_ROOT."sift_users/".$username)) {
if(is_file((APP_ROOT."sift_users/" . $username . "/operations.json"))) {
$operations_file = APP_ROOT."sift_users/" . $username . "/operations.json";
$contents = file_get_contents($operations_file);
$file_contents = json_decode($contents, true);
$get_general_settings = file_get_contents(APP_ROOT. "sift_users/".$username."/general_settings.json");
$general_settings = json_decode($get_general_settings, true);
if(!empty($file_contents)) {
$file_contents = json_decode($contents, true);
$user_operations = array();
$testing = "";
foreach($file_contents as $operation_name=>$op) {
if(!file_exists(APP_ROOT. "sift_users/". $username ."/operations_data")) {
mkdir((APP_ROOT. "sift_users/". $username ."/operations_data"), 0777, true);
}
if(!file_exists(APP_ROOT. "sift_users/". $username ."/operations_data/". $operation_name)) {
mkdir((APP_ROOT. "sift_users/". $username ."/operations_data/". $operation_name), 0777, true);
}
$results_filename = 'results_'.date('m-d-Y');
$search_results = social_search($operation_name, $general_settings);
if(!file_exists(APP_ROOT. "sift_users/". $username ."/operations_data/". $operation_name."/".$results_filename)) {
print_r( json_encode($search_results) );
file_put_contents((APP_ROOT. "sift_users/". $username ."/operations_data/". $operation_name."/".$results_filename), json_encode($search_results));
chmod((APP_ROOT. "sift_users/". $username ."/operations_data/". $operation_name."/".$results_filename), 0777);
} else {
$results_file = APP_ROOT. "sift_users/". $username ."/operations_data/". $operation_name."/".$results_filename;
$old_contents = file_get_contents($results_file);
$old_results = json_decode($old_contents, true);
$duplicate_result = false;
$verified_results = array();
foreach($search_results as $new_key => $new_result) {
foreach ($old_results as $old_key => $old_result) {
if($old_result['hash_id'] == $new_result->hash_id)
$duplicate_result = true;
}
if(!$duplicate_result) {
array_push($verified_results, $new_result);
}
}
foreach($old_results as $key => $result) {
array_push($verified_results, $result);
}
print_r( json_encode($verified_results) );
file_put_contents($results_file, json_encode($verified_results));
/*$fh = fopen($results_file, 'w'); // or die("can't open file");
$stringData = json_encode($verified_results) ;
fwrite($fh, $stringData);
fclose($fh);
print_r(preg_last_error());
*/
}
//chmod((APP_ROOT. "sift_users/operations_data/". $operation_name."/testing.json"), 0777);
$user_operations[$username][$operation_name] = $op;
$testing.= $operation_name." - ";
//deleteDir(APP_ROOT. "sift_users/". $username ."/operations_data");
}
}
}
}
}