PHP forced download going to webpage - php

I have an issue with the force download of a file.
I'm using PHPExcel to create a xls file based on information extracted from our database. This all works fine and the Excel file works as required.
I then attempt to force download of created file using the following function. However it downloads webpage rather than as a file.
Currently developing on Win XP SP3, Notepad++, XAMPP (Apache 2.4.3, PHP 5.4.7).
**function following
public function downloadfile($file){
if(file_exists($file) && is_file($file)){
//ob_start();
echo $file;
echo "in file exists";
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();
readfile($file);
//ob_end_flush();
}else
echo "file or location does not exist <br>";
echo $file;
}
Any help would be appreciated.
Thanks in advance

If its a excel file then just redirect to that url and by default it will ask for file download.
header("Location: http://site.com/path/to/file/filename.xlsx");
/* Make sure that code below does not get executed when we redirect. */
exit;
If in javascript give
location.href = 'http://site.com/path/to/file/filename.xlsx';

header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $fileName . ".xls\"");
header("Pragma: no-cache");
header("Expires: 0");
readfile($filePath);
try this

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 force Download - downloading corrupted file

Hi I am trying to allow download a demo file which is related to a product. my code is like this
if(isset($_POST['submit'])){
$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/download/";
$path = $root.$path;
$filename = $demo;
$file = $path.$filename;
header('Content-Type: application/force-download');
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Length: ".filesize($file));
readfile($file);
}
I am able to get file name which is associated with the product by
$demo
After user submits his information, download will start automatically. Now this is downloading a non existing file or corrupted file with the proper file name. please help
<?php
$file = 'monkey.gif';
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;
}
?>
As you can see Content type is application/octet-steam meaning file is byte by byte encoded. Also the cache headers are set. Then headers are forcefully sent by ob_clean();flush(); and then the file is read.
The file_exists is there to ensure that given file exists. You should also try not not thrust user input as they could easy write names for your php codes and download EACH file. And with ../ in names of the files, even your documents or system files and so on.

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.

Download file once - Corrupt file error

What I want to do is to allow only one download per user (one access to a href) For that I have a variable in the user's table, which I do change when the link has been clicked.
I use "download.php?file=file.xxx" for doing that.
download.php
$file= basename($_GET['file']);
$root = "documents/rece/";
$path= $root.$file;
echo $path;
if (is_file($path))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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($path));
readfile($path);
}
else
echo "File error";
exit();
?>
I also update the DDBB and that works. After that I can show or hide the link. The problem is that the downloaded file is corrupted and can't be opened. I will use it with pdf or doc, maybe zip.
Could it be because of the path?
PDF files, as far as I know, start with something like this:
%PDF-1.4
Yours start with 4 blank lines plus documents/rece/Form.pdf%PDF-1.4. You're obviously printing it yourself somewhere before the code you've posted.

How to download a PDF file instead showing it in Adobe Reader?

I'm trying to create a download link for a PDF file. It works fine if Adobe Reader is installed, but if I uninstall it, it tries to open in browser and fails.
Please tell me what is the actual problem about this?
Thanks in Advance.
The problem is that when adobe reader is installed than it automaitcally gets the header of .pdf file and show the file.
You can use this.It will always prompt to download the file ..
$path="uploads/";
$actualfilename=$path.$row["name"];
//name of the file or location of file..
if($typeofview=="download") {
#readfile($actualfilename);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $actualfilename. '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($actualfilename));
header('Accept-Ranges: bytes');
exit;
}
This is a duplicate of this question but here is the answer:
$path_to_file = '/var/www/somefile.pdf'; //Path to file you want downloaded
$file_name = "somefile.pdf"; //Name of file for download
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
readfile($path_to_file);
die();

Categories