lost connection between PHP server and client while downloading a file successfully - php

This is my code:
error_log('download start');
readfile('setup.exe');
error_log('download complete');
The file was received successfully but the third line is not executed, its not log the 'download complete'
We facing this problem for a long time we tried also to send chunks (using flush) of this file and after several chunks the connection lost so the rest executable code is not running (but the file received).
We know that the connection is lost by using connection handling in php.
We also set connection time limit to max.

You need to set certain headers first, binary, etc:
$file = 'setup.exe';
if (file_exists($file)) {
error_log('download start');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
$result = readfile($file);
if ($result) {
error_log('download complete');
}
else {
error_log('unable to download');
}
exit;
}

Related

Can't play video file with more than 700mb that downloaded using XAMPP

I can't download files that already uploaded in my website, for example :
I already uploaded a video file with 800mb file size and it is okay, the file is save to my directory in the File folder, then I want to download that file again, but as soon as I download the file, I will always got 1kb file not the exact size of the file, and when I play it, nothing happen
this is my code:
<?php
include 'db.php';
if(isset($_REQUEST['name']))
{
$var =$_REQUEST['name'];
$dir = "../files/";
$file = $dir . $var;
if(file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: video');
header('Content-Disposition: attachment;
filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
mysqli_close($conn);
exit();
}
else{
echo "File not found";
}
}
?>
Yeah, I had the same problem too. After searching alot on the internet, I found out that the problem is related to output buffering. The following code solved my problem.
<?php
$file = $_GET['file'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_end_clean(); //adding this line solves my problem
readfile($file);
exit;
?>
The code ob_end_clean() basically runs grabs everything in the buffer, then erases the buffer, and turns off output buffering.

PHP headers to serve ZIP. It downloads but is corrupt... why?

Good morning,
I'm working on serving a zip file through the PHP headers, which downloads, but every time I try to extract it shows that the zip is corrupt... I need a 2nd pair of eyes to look over this... what am I missing? Thank you so much!
$file_download = 'example';
if (isset($file_download)) {
$file = 'path/to/file/'.$file_download.'.zip';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
} else {
echo 'File does not exist!';
}
}

Simple PHP script for XZ file download

I'm trying to setup a backup system that will perform a backup, compress the file and make it downloadable from a browser. The file downloads properly but when I try to uncompress it I get:
unxz: backup_2015-08-12.txz: File format not recognized
There is a backup script that will output
/tmp/agribackup.txz
The PHP script is as follows:
<?php
// Create the agribackup.txz file
$e = shell_exec("/usr/local/bin/agribackup.sh");
$file = '/tmp/agribackup.txz';
if(file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/xz');
header('Content-Disposition: attachment; filename=backup_'.date("Y-m-d") . '.txz');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
I think you are using the wrong Content-Type. Refering to 'http://tukaani.org/xz/xz-file-format.txt', the correct MIME-type is 'application/x-xz'.

Download file program using php on unix box

I have created a beginner program to forcefully download file from unix box to windows through browser, it is not throwing any error but shows nothing on browser just a blank page.
PHP version- 5.2.13
Apache-2.0
Unix Box- HP-UX 11.11 (old version latest is 11.31)
local PC- windows XP Prof.
Browser- IE 7, Mozilla.
Below is my code (this code resides on unix box):
<?php
ob_start();
$file = '/opt/hpws/apache/htdocs/barn/file2';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream;charset=utf-8');
header('Content-Disposition: attachment; filename=$file');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
This line had quotation marks missing:
header('Content-Disposition: attachment; filename=$file');
and in trying to use that line of code, the browser would prompt to save the file as $file.
The line of code should read as:
header('Content-Disposition: attachment; filename="'.basename($file).'"');
The following (tested with a binary file) with file inside the same folder as executed code.
NOTE: You could use header("Content-Type: application/text"); if it's an ASCII file.
<?php
ob_start();
$file = 'file.zip';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream;charset=utf-8');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
Okay, let's add some checks and debugging.
<?php
$file = '/opt/hpws/apache/htdocs/barn/file2';
if (!file_exists($file)) {
die("The file does not exist");
}
if (!is_file($file)) {
die("Not a file"); // Worry about symlinks later
}
if (!is_readable($file)) {
die("The file is not readable");
}
die("DEBUG: Okay, will send the file -- remove this line and retry");
$name = basename($file); // Or anything else
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"{$name}\"");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit();
?>
If this does not work, it should at least tell you why. Also, on the first run, the one when it will still not download the file but only tell you that it will, check that the page does not contain anything else except that one line. Otherwise, you're setting yourself up for a fall; if not this once, as soon as you have to send a file larger than your output buffers, or too many files for your system memory.

sudden database disconnection in following code

I am recording the time at which the user downloaded a specific file using the following code. However, in this code, initially the download time is coming but later it's disconnecting the data base connection between client and server. If I remove the 'exit' (as shown), everything is coming fine but the downloaded file can be corrupted or damaged.
Can anyone check this code and explain what is wrong with it? I think the problem is with the exit, but what can I use instead of exit?
<?php
$f_name = $_POST["fn"];
$file = "../mt/sites/default/files/ourfiles/$f_name";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
//ob_clean();
// flush();
readfile($file);
// exit;
}
$con = mysql_connect("localhost","mt","mt");
if (!$con) {
die('Could not connect: ' . mysql_error());
} else {
echo "Connected";
}
// Create table
mysql_select_db("mt", $con);
mysql_query("INSERT INTO down_time (FileName,DateTime)
VALUES ('".$f_name."',NOW())");
mysql_close($con);
?>
If that happens on large files and/or slow connections, try to tweak max_execution_time in php.ini or from script using ini_set function.
well if you include the exit, your code just doesn't come to the point where it should insert the filename into db.
if you don't include the exit, you send the file contents and append "Connected" to it so the file has to be corrupted.
maybe you can try ob_start and ob_end_clean around your db stuff: http://php.net/manual/en/function.ob-start.php
this prevents anything from being sent to the output, so you don't have to use exit but nothing gets sent to the output after your file so it doesn't get corrupted
something like:
readfile($file);
}
ob_start();
$con = mysql_connect("localhost","mt","mt");
//all the DB stuff
mysql_close($con);
ob_end_clean();
?>
you can include exit after the ob_end_clean() just to be sure but this should work just fine.
Try:
<?php
$f_name = $_POST["fn"];
$file = "../mt/sites/default/files/ourfiles/$f_name";
if (!file_exists($file)) { die('File not found'); }
if (!$con = mysql_connect("localhost","mt","mt")) { die(mysql_error()); }
if (!mysql_select_db("mt")) { die(mysql_error()); }
$q = "INSERT INTO `down_time` (`FileName`, `DateTime`) VALUES ('"
. mysql_real_escape_string($f_name) . "',NOW())";
if (!mysql_query($q)) { die(mysql_error()); }
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($file);

Categories