Make the script wait for the generation Sparse file


I have a script that merges 2 files, and uploads it

1) Small file - LITERAL

2) LARGE FILE (where size<=2GB) - GENERATED

    # $file_size is <= 2GB

    # generate sparse
    $block_size = $file_size / 10;
    exec("dd if=/dev/zero of=sparse bs=" . $block_size . " count=0 seek=" . ($file_size - 32768),$tmp_array);


    # merge files
    exec("cat temp_file sparse > mergedFile");

    # remove temporary files
    exec("rm temp_file sparse");

    # upload the file now

My problem is, when it is merging the files, it takes a lot of time.

The merging process is skipped and the file is already uploaded, which should not be done until the whole Sparse file and the temporary file is Merged.

thanks for helping


exec("cat temp_file sparse > mergedFile"); isn't waiting for the command to finish because you are redirecting the standard output of the cat command to a file. If you run a command like this in exec(), the command will run in the background and PHP will move immediately to the next line. Two possible solutions: either wrap the cat temp_file sparse >mergedFile inside of another shell script or do the file concatenation within PHP itself.