0

I have a php function, which sends a linux command to zip a number of selected files. The file is then downloaded.

$command = "zip data ";

foreach($_POST['checkbox'] as $key=>$llogfile){     
    $command = $command . $llogfile . ' ';
}

$stream = ssh2_exec($connection, $command);
stream_set_blocking($stream, true); 
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);

//sleep(1);

$file = 'data.zip';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$filetype = finfo_file($finfo, $file);

header('Content-Description: File Transfer');
header('Cache-Control: public');
header('Content-Type: '.$filetype);
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename='. basename($file));
header('Content-Length: '.filesize($file));
ob_clean();
flush();
readfile($file);

If sleep() is disabled, the .zip file is downloaded with no files. I think it might be because of the processing time to zip files at the server.

My isse is the following: Since the user chooses the amount of files to be zipped, zipping time may vary, therefore, sleep(999) doesn't seem a good solution and sleep(1) might not work properly when large amount of files are chosen.

Is there any easier way to wait for the command to be executed before downloading the file?

Important: I cannot zip files on php side, it has to be done this way. Security is not important since this is being used for personal purposes.

Ching
  • 25
  • 4

1 Answers1

0

Sorry, can't comment yet but:

Waiting for command to finish using SSH2_Shell in PHP

so you can do the same:

 while (true){
        $data .= stream_get_contents($stream);
        if (strpos($data,"XOXO") !== false) {
            echo "okay: command finished\n";
            break;
        }
    }
    echo $data;
    fclose($stream);

just replace "XOXO" with something that zip command return to the stream

hope it is helpful

Community
  • 1
  • 1
Alex
  • 16,739
  • 1
  • 28
  • 51