简体   繁体   English

PHP 写入文本文件从字符串末尾截断文本

[英]PHP writing text file cuts text off from end of string

In my code I have to write multiple json strings to files.在我的代码中,我必须将多个 json 字符串写入文件。 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.我最初尝试使用fwrite() ,然后仅使用file_put_contents()进行测试,但是两种写入文件的方法都会切断字符。 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.我知道 json 字符串在写入后是完整的,因为print_r在将其写入文件之前会返回完整的字符串。


The text that's being cut off is: 被截断的文字是:
108 characters

 5","elapsedTime":"11 hours ago","hotel_cat":null,"visible":true,"hash_id":"22aa8da3d0b8ef44ec07f8521986fbac"}]


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).第一个文件大小为 47KB,截断点在随机点留下 108 个字符(不是因为无效字符)。

The second file is 52KB in size, and the cut off point leaves 110 characters, once again, at a random point.第二个文件大小为 52KB,截止点再次在随机点留下 110 个字符。

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('md-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"); } } } } }

An old question but anyone else scrambling for a fix,一个老问题,但其他人都在争先恐后地寻求解决方案,

For me on my Dev server I changed the following in the PHP.ini file: max_input_vars = 5000 and up'd both the input time and memory limit:对我来说,在我的开发服务器上,我在 PHP.ini 文件中更改了以下内容: max_input_vars = 5000 并增加了输入时间和 memory 限制:

max_input_time = 180 memory_limit = 512M max_input_time = 180 memory_limit = 512M

Pretty sure only the input vars was needed but not tested it很确定只需要输入变量但未对其进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM