<?php
// force to download a file
if(isset($_POST['download'])){
$file = "images/dansyo_logo.png";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));
header( "Content-Description: File Transfer");
#readfile($file);
if(#readfile($file)){
echo'proceed';
}else{
echo'failded';
}
}
?>
<form method='post'>
<button name='download'>download</button>
</form>
I have the above code where i want the code to be able to download a file and in the same time insert values into my database.The code is functioning well that is i'm able to download a file.However,nothing is echoed after the file has downloaded or is downloading.
Its not going to echo since header will send the file to browser for download, if you wanted to do any kind of db submission just do it before header sent
$file = "images/dansyo_logo.png";
//Do your DB update here, before the header
if(is_readable($file)) {
// insert into database
} else {
exit(basename($file)." not found.");
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Description: File Transfer");
#readfile($file);
exit();
Note: file_exists() will return true on a directory.
Related
I have a site with my hiking-tours:
https://pknudsen.net/gps/gpxviewer.php?gpstur=777
The routes are shown on a Google map. Google requires filetype "xml". I want to make a download-function so the users can get the tours to their gps. It requiers "gpx" file types. The download code is:
function myFunction($navn) {
$path = $navn;
$mm_type="application/octet-stream";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
}
if (isset($_GET['name'])) {
$navn = $_GET['name'];
myFunction($navn);
}
How do i change the extension to "gpx"? I don't want to write the gpx-file on my server. Just to the user.
Problem with making zip file not able to create and download show only zip name in response
if (file_exists($filename)) {
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
$mimeType = 'application/json';
header("Content-type: " . $mimeType);
header("Content-Disposition: attachment; filename=\"{$filename}\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . mb_strlen($filename));
echo $filename;
flush();
readfile($filename);
// delete file
unlink($filename);
}
Try to set the Content-type like this: header("Content-type: application/zip");
i am trying to make the video seek using php file
this is my code
<?php
$path = 'http://exmaple.com/video.mp4';
$mm_type="video/mp4";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary
");
readfile($path); // outputs the content of the file
exit();
?>
the video work but it is not possible to seek
Seeking requires that your server support handling of the Content-Range: header
I'm trying to download a CSV file through the browser. The script is partially working, because so far I managed to display the CSV on screen, but the download is not starting.
Here is what i tried so far:
if(isset($currency)) {
header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=Pricelogs.csv");
ob_clean();
$filename = "/tmp/".uniqid().".csv";
exportCSVFile($filename, $currency, $country, $provider);
readfile($filename);
//unlink("'".$filename."'");
} else {
echo "ERR_USERNAME_PASSWORD";
exit();
}
I had already read all questions of this type from FAQ & also tried but it gets open on browser only,instead of downloading.
I had also used header with single quotes.
I also tried header("Content-Type: text/csv");
Also:
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
PUT your CSV file URL, it will display in browser in place of force browser to download. You can open it in iframe in your site.
http://docs.google.com/viewer?embedded=true&url=www.yoursite.com/filename.csv
I usually just do:
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=my_csv_filename.csv");
// print CSV lines here
exit();
I can tell you this combination is working for me:
$bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
header("Content-type: application/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $bom . $data;
exit;
If I were you, I would first test this plainly (outside of all your buffering), first see that you manage to make this work, and only then test it in your specific set up.
You might try this.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
print $content;
For large files you need to get your output buffer started
add : ob_start(); at the start
ob_clean(); at the end of you file
I wanna make a link for download and all of my file is zip but idont want to my user knows where is my download link
so i wanna get content of zip file and echo it with zip header to user can download it how can i do this?
You should use a "download.php?get=". base64_encode($filename)
When the user click on "download zip", you redirect him with header("Location: download.php?get=". base64_encode($url) to that page.
The headers, here we go
$filename = base64_decode($_GET[get]);
$filepath = "/var/www/domain/httpdocs/download/path/";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
Instead of experimenting with headers you could try downloading some 'ready for use' php class. Then your could do something like:
$fileDown = new PHPFileDownload(null, null, 'myFileName', null, 'mypath/myfile.txt');
$fileDown->exportData();