0

I need to join two csv files and then read entire merged file.
the problem is php keeps executing function when fwrite doing it's job.
so when fwrite starts a pause/halt is needed.

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

when i run Doo::dictionary() then language_pack.csv file only contain the first csv file content.

mhesabi
  • 1,140
  • 3
  • 22
  • 48
  • 1
    From my knowledge PHP isn't multi-threaded, so unless it calls through the command line interface it should execute as a single process. http://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications – Aram Kocharyan Feb 17 '13 at 15:30
  • because when I comment `return` line on `function dictionary` and run the application `language_pack.csv` is just fine. may be something is wrong with my code... – mhesabi Feb 17 '13 at 15:45

0 Answers0