Reputation: 23
i have a big problem with my php code, i use a html form for get the 'filename', work perfectly, BUT my problem is : when i launch the download, all browser's download zip files, and get Network error, ex : 578ko / 600ko : Network Error.
<?php
$dir = "lol/"; // trailing slash is important
$file = $dir .$_POST['filename'] ;
if (file_exists($file)) {
header('Pragma: public');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Transfer-Encoding: binary");
header("Content-type: application/zip");
header('Content-Disposition: attachment; filename='.basename($file));
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
readfile($file);
} else {
echo "Le fichier $file n'existe pas.";
}
exit;
?>
Upvotes: 2
Views: 1490
Reputation: 468
Check your web server timeout values and increase/define to higher value. Also turn off output buffering.
<?php
$dir = "lol/"; // trailing slash is important
$file = $dir .$_POST['filename'] ;
//Turn off output buffering
if (ob_get_level())
ob_end_clean();
if (file_exists($file)) {
header('Pragma: public');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Transfer-Encoding: binary");
header("Content-type: application/zip");
header('Content-Disposition: attachment; filename='.basename($file));
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
readfile($file);
} else {
echo "Le fichier $file n'existe pas.";
}
exit;
?>
Upvotes: 5
Reputation: 33804
You can try reading and sending chunks - it might help
<?php
$dir = "lol/"; // trailing slash is important
$file = $dir . $_POST['filename'] ;
if( file_exists( $file ) ) {
header('Pragma: public');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Transfer-Encoding: binary");
header("Content-type: application/zip");
header('Content-Disposition: attachment; filename=' . basename( $file ) );
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize( $file ) );
/*
send the file in chunks rather than trying to read and send all at once
*/
if( $fh = @fopen( $file, 'rb' ) ) {
while( !@feof( $fh ) and ( connection_status()==0 ) ) {
print( fread( $fh, 1024*8 ) );
flush();
}
@fclose( $fh );
}
} else {
echo "Le fichier $file n'existe pas.";
}
exit;
?>
Upvotes: 1