OK, I've had a good read through the "Related Questions" section and I haven't found this answer.
I'm using an ajax request to force a php download. Things are working fine on the PHP end. Let's say I've got a stream of data called DATA. Now, I want to pop up a "Save as..." dialog.
The browser has received a string of hex values. Now, what do I do with this DATA on the client (javascript) side?
This is the PHP code that I'm using, per the link above:
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=$file');
readfile($file);
I've tried
window.open(DATA) -> hex stream doesn't exist (of course)
submitting a form with the action as DATA -> same problem
BTW, If I echo the file from PHP then use window.open, it works sometimes. But not for txt files or jpgs etc.
I've seen this working on other sites - how are they doing it? Thanks in advance.
Here's the answer I was looking for:
window.open("downloadPage.php");
...which pops up a box every time. The problem with the ajax request was that the returned file stream was interpreted as XMLHttpRequestObj.reponseText.
The browser apparently interprets this differently and doesn't allow the download.
Related
I'm outputting a PDF via PHP with the following code. $file is an object that contains data pertaining to the file being displayed.
header('Content-type: application/pdf');
header('Content-disposition: inline; filename="'.$file->name.'"');
#readfile($file->ServerPath());
My issue is that when I go to download the file in Chrome it will occasionally try to save the page instead of the PDF.
For example, say the URL that is displaying the file is mywebsite.com/file?file_id=1234. Most of the time it will try to save the file correctly as "file_name.pdf". However, sometimes chrome will try to save the file as "file" with no extension. This seems to happen randomly.
If it makes any difference the page displaying the file is being opened in a new tab. The issue happens regardless of whether I redirect via PHP or Javascript.
I really need to resolve this issue, as these PDFs will be accessible by users.
Thanks in advance.
I have a PHP application that generates a CSV file and redirect the user to a static page linking to the file, just the example below :
https://www.example.com/public_html/static/temp/myfile.csv
Problem is, Chrome is opening the file instead of saving it. I need Chrome to save this file, as it would do with any other file like a zip or mp3, for instance.
Here is what I tried :
header('location:https://www.example.com/public_html/static/temp/myfile.csv');
header('Content-Disposition: attachment; filename=myfile.csv');
But no luck, Chrome keeps showing the myfile.csv contents instead of downloading it.
Any ideas ?
Thanks
Your argumentation in the comments has one never-ending misunderstanding: the Location header instructs any client to perform a new request to the given URI. With that the current request is over. Headers from the current request (i.e. Content-Disposition) aren't magically carried over to the next request.
In other words: your "static page linking to the file, just the example below" must send your wanted header.
Ultimately I'm sure it's not a Chrome problem either, but affects all internet browsers, as they easily detect the CSV data as text, hence being able to render/display that data instead of only being able to save it to a file.
With html5 you can set the "download" attr in an element.
Download it!
Source : http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
After struggling with this issue for some days, the only real solution i got is to ZIP the file and then redirecting to the ZIP file instead of the CSV. By doing this, Chrome will download the ZIP file instead of opening it :
header('location:https://www.example.com/public_html/static/temp/myfile.csv.zip');
I am looking to see if such a scenario is possible -
My website hosts couple of mp3 files. The URL would look like www.abc.com/.mp3 .
Assume that I get an incoming request from a user on iOS/android, using his browser to access my mp3 link above
My question is, can i listen to the event (that of my website mp3 being accessed) and then send send another mp3 to the user?
I am looking at using PHP and javascript, maybe, to do this. Please direct me on the approach to do this.
If you want to use php or javascript as said in your question, I can imagine two solutions for you problem on client side (javascript) or server side (php) :
On client side, using javascript, you can wrap the call to the mp3 in a javascript function (maybe by ajax). This function will check the browser and depending on it get the proper mp3
On server side, using php, you may wrap the mp3 query in a PHP script, say getmp3.php?file=xxx.mp3. Your client page will not get the mp3 directly but ask it to this php script. This script will check User Agent and send the mp3 with something like :
// Get the asked mp3
$askedfile = _GET['file'];
// Get browser info
$browser = get_browser(null);
// Put filename to the proper mp3 file depending on the browser
$filename = ...;
// Put the proper content/type :
header('Content-Type: audio/mp3');
header('Content-Disposition: attachment; filename="$askedfile"');
header('Content-Length: '.filesize($file));
// Send the mp3
readfile($filename);
My PHP script is outputting the contents of a .sql file, after it has been called by a POST request from my Delphi Desktop Client.
Here is what is happening:
My Desktop Client sends a POST request to my PHP Script.
The Script then calls mysqldump and generates a file - xdb_backup.sql
The Script then include "xdb_backup.sql"; which will print and return it to the Desktop Client, whereafter it deletes the SQL file.
The problem is, that the size of the SQL file can vary (for testing, I generated one that is 6 mb). I would like my desktop client to be able to show the progress, however the PHP script does not expose it's size, so I have no Progressbar.Max value to assign.
How can I make my PHP script let the Client know how big it is before the whole thing is over ?
Note: Downloading the SQL file is not an option, as the script has to destroy it. :)
You would do
$fsize = filesize($file_path);
where $file_path will be path to the generated file xdb_backup.sql,
to get the filesize in server and return headers with the following line attached.
header("Content-Length: " . $fsize);
Take a look at http://www.hotscripts.com/forums/php/47774-download-script-not-sending-file-size-header-corrupt-files-since-using-remote-file-server.html which explains a download php script.
You have to send a Content-Length header using header function. Something like this:
header('Content-Length: '.filesize('yourfile.sql'));
You may want to send the file using readfile instead of include.
You can set the Content-Length header with the size of xdb_backup.sql
well I'm just wondering how I can get an mp3 download to start instantly, as oppose to it simply starting to play in the browser when you directly go to it.
Preferably using php headers.
So essentially when you click the file, I want a download box to appear saving save etc. Right now it just opens and starts playing in the browser.
Thanks
You'll need to create a PHP file that "redirects" to the MP3 file, and point your links to that PHP file.
Code as below:
<?php
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename="fileName.mp3"');
readfile('originalFile.mp3');
?>
Note: The line that sets the Content-Disposition header is the critical one.