I wanted to let a user download a file by simply clicking a button. Thing is, the file doesn't actually exist - its just some dynamic content.
So lets say:
$('a.download').click(function(){
$.post('get.php');
})
and in my PHP:
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=something.txt");
header("Content-Type: text");
header("Content-Transfer-Encoding: binary");
echo 'abcbdefg'
Is that valid? Is there some other way to do it?
Just create a link to the file, like this:
download my file
Whenever there's a request for a file of type PHP, your webserver will first process the file and output whatever text it contains to the client; you don't have to do anything special just because it's dynamic.
Using $.post() doesn't make sense for what you want to do; that POSTs data to the url you specify, it doesn't prompt the user to save a file.
Yeah, that's valid. I'm pretty that's the best way to do it.
Related
I'm creating MS Office Word document with PHP as following:
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=filename.doc");
header("Content-Transfer-Encoding: binary");
But I need to save file to the server without save as dialog to be able to do something with the created *.doc file and give it to user only after the modification.
So how can I acheive this?
The code you use is for sending as download to the browser user.
To save on the server, use the this in PHP :
file_put_contents('path/to/ms/word/document.doc', $theData);
Then after you use your code to sent the saved file.
I am trying to make a link that is clickable; when it's clicked on it forces a download (or even a Save As option would be nice) of an .xls file. This is what I have below.
When the link is clicked, there are no php log errors and it goes through this code. The file does exist as well. Are my headers wrong?
if (#file_exists("/tmp/report/{$php_session_id}.xls")) {
$filename = "/tmp/report/{$php_session_id}.xls";
$content_length = filesize($filename);
header("Pragma: public");
header("Expires: 0");
header("Content-type: application/vnd.ms-excel");
header("Content-length: {$content_length}");
header("Content-disposition: attachment; filename=\"missing_addresses.xml\"");
readfile($filename);
}
If the code is correct, is it possibly a server that is locked out of doing this?
Also, I am testing on Chrome for Mac (newest version)
UPDATE: I used AJAX which was the problem.
You can't initiate a download through Ajax; the response will simply vanish in the ether, as a normal Ajax response. (You could theoretically capture it, but you won't be able to write it to disk.)
Use
location.href = "source.php";
or
<a href='source.php' target='_blank'>
to direct the browser to the resource directly instead. It will automatically detect that it's a file to be downloaded, and initiate the download.
I have a site which allows clients to download files that they have purchased.
A link is sent to the customer which directs them to a page which checks if the link is valid, and if it is, allows them to download their product.
Here is an example of the link: http://www.psptubestop.com/dl/2z129a2.php?reference=6d556bde201a2fe4079874168&password=535864380ee2bc0f57cced3fe&pid=402
A lot of customers are saying that when they download, the .zip extension is missing, and I can't find the problem, it works fine on my machine, except when I do "save as", but even then some people are having problems.
Is there any way to allow them to download the file, and keep the .zip extension even if they press "save as"?
This is the code I am using to redirect them to the download...
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$originalfilename");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
Thanks for any help.
The problem could be that some files have spaces, in that case with your current code only the first part is used so loosing the extension part, to prevent such error you have to enclose the filename in double quotes, like so:
header('Content-Disposition: attachment; filename="'.$originalfilename.'"');
I am willing to bet it has something to do with there being a space in the filename seeing as the filename in the headers is PSP_Tubes_PSP_Tubes_Lisa_Cree_The Gift_6932.zip and the file wants to be save as PSP_Tubes_PSP_Tubes_Lisa_Cree_The.
Add a dummy GET parameter that looks like a filename.
...&fname=/foo.zip
I have written a script using header function that generates CSV file.Let me explain in detail.
Step 1) I am saving records in mysql.Step 2) I am creating CSV file from saved records from database Step 3) I want to attach that CSV in attachments so that i could send it to different recipents!! I have completed the two steps.But i dont want download functionality in CSV file generation process as i dont want this file available to anyone. My code looks like this:
$file_name = "Register_" . date('l');
$file_name.=".csv";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
Where do I need to make changes?
just remove all calls to header() and the last print
You could do what you try to by using the ob_buffer functions and 'catching' the generated output. However the whole approach is a little strange:
the header functions have nothing to do with creating emails. They serve the purpose to specify how a browser should handle transferred data. 'Cause this is what you do: you send headers and data. Don't.
Instead: write the csv data into a buffer or file and create an email using one of the available email classes (google...). Then take that crafted email and send it. No header() function required for that.
You can use AddStringAttachment:
$file_name = "Register_" . date('l') . ".csv";
$excelContent = "$header\n$data";
$phpmailer->AddStringAttachment($excelContent, $file_name, 'base64', 'application/vnd.ms-excel');
I am creating a PDF file from raw binary data and it's working perfectly but because of the headers that I define in my PHP file it prompts the user either to "save" the file or "open with". Is there any way that I can save the file on local server somewhere here http://localhost/pdf?
Below are the headers I have defined in my page
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");
If you would like to save the file on the server rather than have the visitor download it, you won't need the headers. Headers are for telling the client what you are sending them, which is in this case nothing (although you are likely displaying a page linking to you newly created PDF or something).
So, instead just use a function such as file_put_contents to store the file locally, eventually letting your web server handle file transfer and HTTP headers.
// Let's say you have a function `generate_pdf()` which creates the PDF,
// and a variable $pdf_data where the file contents are stored upon creation
$pdf_data = generate_pdf();
// And a path where the file will be created
$path = '/path/to/your/www/root/public_html/newly_created_file.pdf';
// Then just save it like this
file_put_contents( $path, $pdf_data );
// Proceed in whatever way suitable, giving the user feedback if needed
// Eg. providing a download link to http://localhost/newly_created_file.pdf
You can use output control functions. Place ob_start() at beginning of your script. At the end use ob_get_contents() and save the content to a local file.
After that you can use ob_end_clean() or ob_end_flush() depending on whether you want to output PDF to browser as well, or you would redirect user to some other page. If you use ob_end_flush() make sure you set the headers before flushing the data.