Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make multiple files to force-download

Tags:

arrays

php

header

Sorry if the title doesn't explain much. Let me try to explain further.

I have code that looks like this:

<?
//Grab the number in count.txt
$count = intval(file_get_contents('count.txt'));
//Move the number up one
file_put_contents('count.txt', ++$count);
$number = file_get_contents('count.txt');
//Force Download
header('Content-Disposition: attachment; filename=DataFiles'.$number.".csv");
header('Content-Type: application/octet-stream');

//The data

foreach($array as $info){
echo $info."\n";
}
?>

With $array being an array of data.

Now sometimes the amount of data can be more than 5000, so if the data is over 5000, make another file for every 5000 data that is being echoed. Ea: If there is 20,000 pieces of data in the $array, then it will make a total of 4 files.

like image 227
user1687621 Avatar asked May 06 '13 00:05

user1687621


People also ask

How do I download multiple files at once?

Hold CTRL and click on the files you want to download. Once you have selected the files you want, right click on the last file you selected and select download.

What does it mean when a site tries to download multiple files?

This is actually a warning from your Chrome browser. It means that you have OCLC's download site set to "Ask" when trying to download. You can change this by adding an exception for oclc.org and worldcat.org into the Chrome > Settings > Privacy and Security > Site Settings > Automatic Downloads setting.


2 Answers

You cannot send more than 1 file in response to an HTTP request.

What I would suggest is zip the file in a single file and return that.

See: Download multiple files as a zip-file using php

like image 51
sergserg Avatar answered Sep 19 '22 15:09

sergserg


Here you are how to download multiple files... The tricks is rendering many iframe in same page. Every frame has different source that force download different files

<?php
for($i = 0; $i<5; $i++){
    echo '<iframe src="test_multiple_downfile.php?text='.$i.'">/iframe>';
}
?>

test_multiple_downfile.php content is this:

$out = $_GET['text'];
header("Content-Type: plain/text");
header("Content-Disposition: Attachment; filename=testfile_".$out.".txt");
header("Pragma: no-cache");
echo "$out";
like image 27
fuxas Avatar answered Sep 23 '22 15:09

fuxas