我需要加入两个 csv 文件,然后读取整个合并文件。
问题是 php 在fwrite
工作时一直在执行函数。
所以当fwrite
开始时需要暂停/停止。
static function dictionary($path) {
$language_pack = Doo::conf()->SITE_PATH . Doo::conf()->PROTECTED_FOLDER . 'languages/' . $lang . '/language_pack.csv';
Doo::joinFiles(array($lang_path, $common_lang_path), $language_pack);
// a pause must be done here until Doo::joinFiles finish it's job
return Doo::translator('Csv', $language_pack, array('delimiter' => '|','enclosure' => '"'));
}
// this function joins array of files and will merge them into single file
static function joinFiles(array $files, $result) {
if(!is_array($files)) {
throw new Exception('`$files` must be an array');
}
$wH = fopen($result, "w+");
foreach($files as $key => $file) {
$fh = fopen($file, "r");
while(!feof($fh)) {
fwrite($wH, fgets($fh));
}
fclose($fh);
unset($fh);
fwrite($wH, "\n"); //usually last line doesn't have a newline
}
fclose($wH);
unset($wH);
}
当我运行时Doo::dictionary()
,language_pack.csv 文件仅包含第一个 csv 文件内容。