Download and change extension in php - php

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.

Related

Download file .ZIP with PHP automatically

I would like to download a file from a folder on my server and automatically start the download without the person seeing the original file link.
Here is my current script, but it downloads an invalid corrupted file.
$filename = "dsk.zip";
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="./zz/'.$filename.'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize("./zz/".$filename));
ob_end_flush();
#readfile("./zz/".$filename);
Can you please help me? The file, dsk.zip, is in the folder zz.
$file = "dsk.zip"; // this is what you will get from a param (i.e. ?file=dsk.zip) or from DB query
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='zz/'.$file");
header("Content-Length: ".filesize($file));
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile('zz/'.$file);

problem with header for making zip not able to download and create

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");

Pseudo Streaming MP4 with php

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

Download A File And Insert Values Into A Database

<?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.

PHP Unable to write the content in PDF

After executing the below code, Browser automatically Downloading PDF Document. When i open that PDF, i am getting below Error.
I got below snippet idea from source
<?php
$filename = "yourfilename.pdf";
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/pdf");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
echo "my contents should display in PDF Document";
?>

Categories