Generate a .txt and then force download [duplicate] - php

This question already has answers here:
How to Automatically Start a Download in PHP?
(5 answers)
Closed 7 years ago.
I have a php function for getting info out of my database. When they go to http://example.com/test/download
I want to create a fake test.txt (text is dynamic) and download it. It's contents should be the equivalent of executing foreach(databaseContent() as $content) { echo $content . '<br/>' } inside of it.
How can I get started on this? (Using php)

You can link to a php document along these lines, which forces a download of type plain text. (Well, suggests to the browser that that should happen, at any rate.)
<?php
header('Content-disposition: attachment; filename=gen.txt');
header('Content-type: text/plain');
echo "this is the file\n";
echo " you could generate content here, instead.";
?>
Of course, pass in appropriate post or get args, to control it the way you like.

Related

Redirect after download xml file [duplicate]

This question already has answers here:
PHP generate file for download then redirect
(11 answers)
Closed 9 years ago.
I use the following code to download an xml file
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="download.xml"');
echo simplexml_load_file('temp.xml');
unlink('temp.xml');
?>
I would like to redirect to index.php after this. How do I do this?
header('location...) and meta refresh do not work
Well because you send the header and content this can't be done.
You have to redirect first and then let the file download on the target site.
Make your website change the url two times with javascript:
<script>
location.href="your_php_file_that_downloads_xml.php";
location.href="/other/site";
</script>

php headers instant download videos [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Force-downloading, from php file
(2 answers)
Closed 9 years ago.
I am trying to make a php script when you load on to that page it downloads a video. However since i do not know anything about headers it seems i can't figure it out so please explain why it works and how header works. I am trying to make the browser download video files.Can someone also explaain the headers and what they do please.
Here is my failing code:
<?php
//Outputing video name
$file_name = $_POST['FileToD'];
//outputting video extension e.g video/mp4
$file_ext= $_POST['FileExt'];
//where the file is kept
$file_path = 'mysever.myadress.com/media/movies/' . $file_name;
header('Content-Type:'.$file_ext);
header('Content-Length:' . filesize($file_path));
header('Content-Description: attachment; filename='.$file_name);
readfile($file_path);
?>
If you want to output a video, then don't start by outputting HTML and then switch to video data as part of the same file. (You can't set response headers after you've started outputting data anyway). Remove everything before <?php and after ?>
$file_url should be the path, on the server's file system, to the file you want to make available. It shouldn't be a URL (unless you want a really inefficient approach or need to proxy from a different server), and if it is a URL then it needs to start with the scheme (e.g. http://).
The content-type needs to be the actual content type of the video (e.g. video/mp4), not a file extension (and it doesn't make sense for it to be provided by the user).
You also need to sanitise the user data. At present (if the errors described above were fixed) then anybody could request any file that exists on the server.

Which header to set or how to download a php file [duplicate]

This question already has an answer here:
php: force download to hd?
(1 answer)
Closed 9 years ago.
i was trying to download from an android application a php file stored onto my server.
From the application i call a webservices that have to give me in output the file .php that i need.
On the web i found this that talk about which header set to download a file. It talks about
zip
jpg
txt
pdf
Then i was thinking to develop something like:
Application call ws to get a php page
Ws zips the php file that application needs and give me in putput
Application download the zip file and extract it.
This is a good solution but i was trying to find something better.
Another solution that is really like what i want is highlight_file
Just use it like:
echo highlight_file("myphpfile.php");
The problem is that in order to render good the file, the code is divided by many html tags and just "visually" is the same file but the source is really different.
Is there a way to download the file directly? Thanks! :)
The simplest solution might be:
<?php
header('Content-Type: text/plain');
readfile("myphpfile.php");
this will display the file as text in browser you can click 'save as...'
If you need the browser to popup a 'Save as...' dialog by itself - without displaying the file - then you'll need the Content-Disposition header:
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="myphpfile.php"');
readfile("myphpfile.php");

Write database to CSV file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - send file to user
I have written a script that successfully outputs userdata to a CSV file on my server.
My question: is there a way to make the CSV file popup so the user can open it? As is, the only way to open the file is directly from the server.
<?php
$csv_file = '/path/to/yourfile.csv'; // path to the csv file you're generating on the server
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="'.basename($csv_file));
readfile($csv_file);
?>

Forcing page download in PHP [duplicate]

This question already has answers here:
How to force file download with PHP
(12 answers)
Closed 2 years ago.
How do I force the browser to download the current page browsed to? A page with the header Content-type: text/plain for example using PHP?
If a user navigates to that page, a download box should appear (the browser download dialog with the usual "Save As".
Straight from http://php.net/header
<?php
// There is contention over if this MIME type is right, but just use it for now.
header('Content-type: text/javascript');
header('Content-Disposition: attachment; filename="file.js"');
readfile('file.js'); // Echo the file
?>
NOTE: this must be done before any other output (and can be about the only thing on the page, unless you want other output in your file).

Categories