0

So here i'm trying to send to the client some files received in a array from javascript. My problem is that i cannot redefine the filename (since its a header). So for example, i wanna send 3 files to the client, the first one is sent, then i got a error for the 2 last. Does anyone have idea how to fix it? Here is my php code:

PHP

<?php
ignore_user_abort(true);

set_time_limit(0); // disable the time limit for this script

$path = "../temp/"; // change the path to fit your websites document structure
$data = escapeshellarg($_GET['File']);
$data = str_replace("\\","",$data); // remove useless characters inserted 
$data = str_replace("\"","",$data); // 
$dl_file = explode(",", $data);

header("Content-type: application/octet-stream");
header("Cache-control: private"); //use this to open files directly
header('Pragma: private');

for($i = 0; $i < count($dl_file); $i++)
{
    $fullPath = $path.$dl_file[$i];
    $path_parts = pathinfo($fullPath);
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"",true);

    if ($fd = fopen ($fullPath, "r"))
    {
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);

        ob_clean();
        flush();
        readfile($fullPath);
    }

    fclose ($fd);
}
exit;
?>

Here is the error i got in logs file of the server :

Warning:  Cannot modify header information - headers already sent in (php file path)
MacGruber
  • 162
  • 2
  • 13
  • Is it even possible to send multiple files in a single stream like this? I don't think it is.... Doesn't the header describe the content of the entire response, meaning it can only describe a single file at a time? – random_user_name Nov 20 '15 at 15:34
  • This is a possible duplicate of http://stackoverflow.com/questions/2339440/download-multiple-files-with-a-single-action Basically, you can't do what you are trying to do and will need to take another route. – Joseph Yancey Nov 20 '15 at 15:38
  • The thing is i already tried to call the server for each file to download, but there is a wierd bug where not all files are send to the user. It look like it had something to do with timing or focus, because if i put an "alert" between each call, all the files were sent. Same thing if i use setInterval with like 5 secs between each. – MacGruber Nov 20 '15 at 15:40

0 Answers0