PHP编写文本文件会从字符串末尾剪切文本

时间:2013-08-18 02:36:57

标签: php file fwrite

在我的代码中,我必须将多个json字符串写入文件。当我这样做时,所写内容的结尾会被看似随意的字符数量所截断。

我最初尝试使用fwrite(),然后使用file_put_contents()进行测试,但两种写入文件的方法都会切断字符。该代码段如下所示:

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());
*/

我知道json字符串在写后是完整的,因为print_r在将其写入文件之前返回完整的字符串。

<小时/> 被切断的文字是:
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"}]

第一个文件大小为47KB,截止点在随机点留下108个字符(不是因为字符无效)。

第二个文件大小为52KB,截止点再次在随机点留下110个字符。

这么小的文件会导致什么原因?

<小时/>

编辑:

(完整代码)

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

0 个答案:

没有答案