File downloaded with headers doesn't work - php

I'm trying to download an audio ogg file with a dynamic url
function getAudio($fileName){
$localFile = __DIR__ . '/../../../shared/call_review/' . $fileName;
$fp = fopen($localFile, 'r');
$fp or die('Could not open file "' . $fileName . '"' . PHP_EOL);
$contents = fread($fp, filesize($localFile));
$contents or die('could not read file');
header('Content-Description: File Transfer');
header('Cache-Control: public');
header('Content-Type: audio/ogg');
header("Content-Transfer-Encoding: binary");
header('Content-Length: '.filesize($contents));
header("Content-Type: audio/ogg");
header("Content-Disposition: attachment; filename=" . $fileName);
echo $contents;
exit();
}
But after file is saved, it doesn't work, or is corrupt.

The problem seems to be this line:
header('Content-Length: '.filesize($contents));
The file size should come from filesize($localFile) as demonstrated several lines earlier,
so it should be:
header('Content-Length: '.filesize($localFile));
Or just use the length of the $contents variable:
header('Content-Length: '.strlen($contents));

Related

How to download file from database/folder using php

File uploaded will be store in database and folder (folder name: uploads).Now I want to know how to create download file from database/folder. I have two file butangDonload.php and download.php. When user click word "donload" the pop up box will appear and save the file.
butangDonload.php
<?php
$file = "Bang.png"; //Let say If I put the file name Bang.png
echo "<a href='download.php?nama=".$file."'>donload</a> ";
?>
download.php
<?php
$name= $_GET['nama'];
download($name);
function download($name) {
$file = $nama_fail;
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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
?>
I have changed to your code with little modification will works well.
Here is the code:
butangDonload.php
<?php
$file = "logo_ldg.png"; //Let say If I put the file name Bang.png
echo "<a href='download1.php?nama=".$file."'>download</a> ";
?>
download.php
<?php
$name= $_GET['nama'];
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("your_file_path/".$name); //showing the path to the server where the file is to be download
exit;
?>
Here you need to show the path from where the file to be download.
i.e. will just give the file name but need to give the file path for reading that file. So, it should be replaced by
I have tested by using your code and modifying also will works.
here is the code to download file with how much % it is downloaded
<?php
$ch = curl_init();
$downloadFile = fopen( 'file name here', 'w' );
curl_setopt($ch, CURLOPT_URL, "file link here");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 65536);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'downloadProgress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt( $ch, CURLOPT_FILE, $downloadFile );
curl_exec($ch);
curl_close($ch);
function downloadProgress ($resource, $download_size, $downloaded_size, $upload_size, $uploaded_size) {
if($download_size!=0){
$percen= (($downloaded_size/$download_size)*100);
echo $percen."<br>";
}
}
?>
You can use html5 tag to download the image directly
<?php
$file = "Bang.png"; //Let say If I put the file name Bang.png
echo "<a href='download.php?nama=".$file."' download>donload</a> ";
?>
For more information, check this link
http://www.w3schools.com/tags/att_a_download.asp
butangDonload.php
$file = "Bang.png"; //Let say If I put the file name Bang.png
$_SESSION['name']=$file;
Try this,
<?php
$name=$_SESSION['name'];
download($name);
function download($name){
$file = $nama_fail;
?>
You can also use the following code:
<?php
$filename = $_GET["nama"];
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
readfile("your file uploaded path".$filename);
exit();
?>
function downloadFiles($dir, $file) {
header("Content-type: application/force-download");
header('Content-Disposition: inline; filename="' . $dir . $file . '"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: " . filesize($dir . $file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$dir$file");
}
$dir = "folder_path";
$file = "image_name.jpg";
$download = downloadFiles($dir,$file);
Try this way, it is helpful for me.

View PDF in browser rather than download

There are meetings on this website and each meeting has a PDF attached showing the meeting minutes. The next meeting that takes place dynamically collects this attached PDF and allows for a download as the previous minutes. This is all working perfectly, however, when clicking the link for the previous minutes it always opens up a save as box. I was wondering how to get this to just display in the browser (assuming the user has a PDF viewer enabled)?
Here is the function from the controller:
public function current_pdf($id = null, $pdf = null) {
$this->loadModel('Document');
$document = $this->Document->find('list', array(
'fields' => array('Document.dir', 'Document.url')
));
$request_pdf = $this->Meeting->read(null, $id);
if ($pdf == 'current') {
$file = $document[$request_pdf['Meeting']['minutes']];
if (!empty($file)) {
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize(WWW_ROOT . 'files/document/url/' . $request_pdf['Meeting']['minutes'] . '/' . $file));
flush(); // this doesn't really matter.
$fp = fopen(WWW_ROOT . 'files/document/url/' . $request_pdf['Meeting']['minutes'] . '/' . $file, "r");
while (!feof($fp)) {
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
//echo WWW_ROOT.'files/document/url/pdf/'.$file; die;
//readfile(WWW_ROOT.'files/document/url/pdf/'.$file);
exit;
}
$this->redirect(array('controller' => 'meetings', 'action' => 'view/' . $id));
} else {
$file = $request_pdf['Meeting']['current_minutes'];
if (!empty($file)) {
//echo filesize(WWW_ROOT.'files/document/url/pdf/'.$file); die;
/* header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(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(); */
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize(WWW_ROOT . 'files/document/url/pdf/' . $file));
flush(); // this doesn't really matter.
$fp = fopen(WWW_ROOT . 'files/document/url/pdf/' . $file, "r");
while (!feof($fp)) {
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
//echo WWW_ROOT.'files/document/url/pdf/'.$file; die;
//readfile(WWW_ROOT.'files/document/url/pdf/'.$file);
exit;
}
$this->redirect(array('controller' => 'meetings', 'action' => 'view/' . $id));
}
}
And here is the link in the view:
<?php echo $this->Html->link(__('Download Previous Minutes'), array('controller' => 'meetings', 'action' => 'current_pdf', $download, 'current')); ?>
I have tried removing the header("Content-Type...") lines of code and changing attachment in header("Content-Disposition: attachment; filename=" . urlencode($file)); to inline. This stopped the save as pop-up but displayed a load of spurious code in the browser instead.
Any help would be greatly appreciated as I'm currently very confused.
I believe this is as simple as removing this line in your code:
header("Content-Disposition: attachment; filename=" . urlencode($file));
This line will server up the file as a download, and not display the content in the browser.
I hope that helps!

pdf file not downloading

I am using following code to start downloading but the pop-up for download is not opening. Instead, it shows the binary format of that pdf file. It's working perfectly on local server but not working hosted server.
$file = '../Notes/chapter01.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
// header('Content-Type: application/octet-stream');
header('Cache-Control: public'); // needed for i.e.
header('Content-Type: application/pdf');
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_end_clean();
flush();
readfile($file);
exit;
}
else {
die('Error: File not found.');
}
This code works.
$file = '../Notes/chapter01.pdf';
$mime_type = 'application/pdf';
$filesize = filesize( $file );
header( "Content-type:" . $mime_type );
header( "Content-Length:" . $filesize );
header( "Content-Disposition: attachment; filename=" . $file );
header( "Cache-Control: private, max-age=15" );
#readfile( $file );
Hope this helps.

How do I properly send a PDF file through PHP?

I am trying to send a PDF file. Here is the code that is executing:
$file_path = '/path/to/file/filename.pdf'
$file_name = 'filename.pdf'
header("X-Sendfile: $file_path");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename='$filename'");
readfile($file_path):
whenever I download the file directly, it is fine. however when I try to download the file via this script, a file downloads that cannot be opened. my pdf reader tells me it cannot open a file of type 'text/plain'. I also tried setting the Content-type to application/pdf , but I get the same errors. What am I doing wrong here?
Have you tried this?
$file = '/path/to/file/filename.pdf';
header('Content-Disposition: attachment; filename="'. basename($file) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
Using readfile() also eliminates any memory issues you might encounter.
try the code below.
header("Content-Type: application/octet-stream");
$file = "filename.pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp)
If the above one does not help you try the below
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(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;
Don't for get to set the file path $file_path as required.

PHP Zip File Empty?

I have a script that creates a zip files from files in a certain directory. After Download, for a lot of users - the zip file is empty. However, for other users - the file isn't empty. Is it something I'm doing wrong?
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$id.'.zip"');
header('Cache-Control: private');
ob_clean();
flush();
readfile("".$id.".zip");
exit;
Better add some error control:
$file = "{$id}.zip";
if (!is_readable($file))
{
die('Not accessible.');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: private');
ob_clean();
flush();
readfile($file);
exit;
Additionally triple check your output buffering configuration vs. readfile.
I recommended this as a suggestion earlier, here is a skeleton of what I am talking about. It is untested as I don't have access to php at the moment.
$filename = $id . '.zip';
$handle = fopen($filename, 'r');
if ($handle === false)
{
die('Could not read file "' . $filename . '"');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: private');
while (!feof($handle))
{
echo fread($handle, 8192);
}
fclose($handle);
exit;

Categories