How to make a file downloadable - php

Am trying to add file download to my site for audio video and pdf files
Am using the php header() function but instead of downloading the output file, it keeps on downloading the php page instead. here is my code.
header('Content-disposition: attachment;
Content-type: audio/mpeg;
filename=thefile.type');

You need to write the contents of the file out, e.g.
header('Content-disposition: attachment;
Content-type: audio/mpeg;
filename=thefile.type');
echo file_get_contents("thefile.type");

This works pretty well
if (file_exists("audio/thefile.type"))
{
if (FALSE!== ($handler = fopen('thefile.type', 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;
filename=audio/thefile.type');
header('Content-Transfer-Encoding: chunked'); //changed to chunked
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//header('Content-Length: ' . filesize('thefile.type')); //Remove
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
else{
echo "<h1>Content error</h1><p>The file does not exist!</p>";
}

Related

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!';
}
}

PHP File is being displayed instead of being downloaded

I am having a problem that I just can't solve and I'm going madness with this!
Basically I have to export a csv file. The site is obviously running on a server, and the idea is that onclick the file is generated and automatically downloaded.
Everything is just fine, however, the file is being download to the server directory instead of the local user machine. I just can't make it.
What is wrong? Thank you very much!
if(!isset($crm_contas))
$crm_contas = new crm_contas;
$resultGetValues = $crm_contas->getExportContas($filtros, $idsToSend);
// filename for download
$filename = "ContasExportadas" . date('Ymd') . ".csv";
$output = fopen($filename, 'w');
foreach($resultGetValues as $row) {
fputcsv($output, $row);
}
fclose($output);
$filename = realpath($filename);
$fp = #fopen($filename, 'rb');
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($filename));
} else {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename='.$filename);
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($filename));
}
fpassthru($fp);
fclose($fp);`
--> This is the action that calls the code above.
echo "<a href='#' onclick='changeFormAction(\"form_list\",\"".$CONF['HOME']."/contas/index.php?view=export\");document.form_list.submit();changeFormAction(\"form_list\",pesquisa);'><img src='".$CONF['HOME']."/images/structure/excel.png' alt='Exportar' title='Exportar'></a>";
Meanwhile, I noticed that error_log is writting down this lines when I try do download.
[21-Apr-2016 16:23:44 UTC] PHP Warning: Module 'imap' already loaded in Unknown on line 0

Streamed file is corrupt

Everytime I use the code below to stream an image file to me the file comes back as corrupt. The image file most certainly is not corrupt though and is fine when I open it through my ftp. Am I missing something?
$fileTry = imagecreatefromstring(file_get_contents($file_path));
if($fileTry === false)
{
die('no file');
}
else
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file_path));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;
}

Is there any way to move a file from server to local directory using?

I search web for a function/command in php/javascript/xul which can transfer a file to local directory from a http server, but didn't find any sufficient answers.
Is there any function/command avaliable to do this job, using any of PHP/Javascript/XUL?
Here is the php script for file download:
if (file_exists($file))
{
if (FALSE!== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
http://php.net/manual/en/book.ftp.php, here is a good example http://www.php.net/manual/en/ftp.examples-basic.php
Try this function http://www.php.net/manual/en/function.ftp-get.php
This cannot be done with javascript, but in PHP you have to send the proper headers and then serve the file like this:
Example: Force download of CSV file
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
readfile("/path/to/yourfile.csv");

file corrupt when click download link

I try to use php to force the image jpg file download, I have implemented eth following code:
html
<a href = "filedownload.php?src=uploads/myimage.jpg&download=true>download this file</a>
download.php
<?php
ob_start();
include_once 'functions.php';
if (isset($_GET['download']) && $_GET['download'] == 'true')
{
$src = sanitizeString($_GET['src']);
header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.basename($src));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
}
?>
Suppose the full path of the image is "www.example.com/smith/topic/uploads/myimage.jpg", I have recieved the right image name and the download window is appeared as well, but the image is corrupt and with 1KB size, any one could tell me why, thanks a lot.
Here you are example how to use readfile function
<?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;
}
?>
You need some code that actually sends over the file. See e.g. readfile or fpassthru
Try to use:
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );

Categories