header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="file.csv"');
echo "blabla";
$Email = new CakeEmail();
$Email->from(array('no-reply#something.com' => 'Something'));
$Email->to($email);
$Email->subject('Something');
$Email->send($textEmail);
How can I output and give the file to download before sending an email? Is it possible? Or I have to create a temporary file for that myself? It works if I don't send an email.
One solution could be:
Create the file and save it to a tempoary location
Send the email with the attachment (using the code you posted in your question)
Use low-level PHP methods to write file contents to HTTP stream (see this answer)
Finally, delete the tempoary file
I think by the looks of the code you've posted, you're nearly there, just got to sort the order out.
I hope this helps.
Related
Hi I'm downloading a file to an app on iOS using the function readfile() on a PHP web service and I want to know if the file is downloaded correctly but I don't know how I can do that.
So what I'm trying is to do some echo to know if the file has been downloaded like this:
echo "before";
readfile($file);
echo "after";
But the response I get is this:
beforePK¿¿¿
Any one knows what does this mean or how can I know if the file is downloaded correctly?
UPDATE:
Yes it's a zip file, here are my headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$ticket");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
You're trying to output the contents of a zip file aren't you?
readfile($file) works the same as echo file_get_contents($file). If you're trying to present someone a file to download, do not add any additional output else you risk breaking the file.
I would also recommend reading up on the header function. That way you can explicitly tell the browser that you're sending a file, not an HTML page that has file-like contents. (See the examples involving Content-Type)
PHP should be setting the correct headers prior to readfile() - this LITERALLY reads the file out to the browser/app... but the browser/app needs to know what to do with it...
Usually you just assume that once the connection has closed that the data is done being transferred. If you want to validate that the file has been transferred fully, and without corruption you'll need to use a data structure like XML or JSON which will:
Delimit the data fields and cause the XML/JSON parser to throw an error if one is omitted, aka the transfer was cut off before it finished.
Allow you to embed more than one piece of data with the response, eg. an MD5 hash of the file that can be re-calculated client-side to verify that the data is intact.
eg:
$file = 'myfile.zip';
$my_data = array(
'file' => base64_encode(file_get_contents($file)),
'hash' => md5_file($file)
)
//header calls
header(...)
echo json_encode($my_data);
exit;
I'm trying to download multiple files using header() in a while loop, but only one file gets downloaded. Why?
while ($row = mysql_fetch_array($sql)) {
header('Content-Type: text/x-vcard');
header('Content-Disposition: attachment; filename=' . $row['name'] . '.vcf');
}
You can only transfer one file from server side at a time. Typical workarounds are:
tar/zip them up into one file on server side.
use javascript to window.open multiple files for download.
This is not possible. The HTTP protocol does not have support for downloading multiple files. The most common workaround is to put the files in a zip archive for the client to download.
headers ca be set only once before outputting any data.
As you loop you set some headers after that you must output something. In the next loop you will not set any headers.
Please read php.net
What i am trying to do is provide a way for an Xls file generated on the client side in js to be downloaded. So I have the xls in a string in js and need to give the user a way to download it and open it in excel.
As i understand the only way to do this is to do it on the server via the content type, so I have tried to provide a php that does a file relay... Here is the php
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"my-data.csv\"");
$data=stripcslashes($_REQUEST['csv_text']);
echo $data;
?>
A request can end up being rather long so for example i might have this request... (actually shortened greatly).
I am not good with php, can anyone suggest a better way to modify this relay script (or better way entirely) to accomplish this?
http://myserver.com/ExcelRelay.php?csv_text=Id%09City%09Phone%09Address%201%09Address%202%09State%09Type%09Employees%09Revenue%09Leed%09Established%09Comments%09Country%09Postal%20Code%09Territory%0A3%09Greensboro%096538227668%09%0978%20Rocky%20Second%20St.%09New%20Jersey%09Remote%09%090%091%09Sun%20Aug%2009%201964%2000%3A00%3A00%20GMT-0400%20%28Eastern%20Daylight%20Time%29%09%22Et%20quad%20estis%20vobis%20homo%2C%20si%20nomen%20transit.%20%0A%20Sed%20quad%20estis%20vobis%20homo%2C%20si%20quad%20ut%20novum%20vobis
Thanks For the Response, The final script was
<?php header("Content-type: application/octet-stream");
header("Content-Disposition: attachment;filename=\"".$_POST['filename']."\"");
echo $_POST['data'];
?>
One word: POST.
Request Url Too Long is a client side error, and one that is (AFAIK) exclusive to IE, these days. If you want to send the data to the server and have it sent back to you as a file, you will have to send the data in the body of the request.
See here for more information.
I have installed PEAR, Spreadsheet_Excel_Writer and OLE. The sample program is executed successfully but when I try to read the file it shows garbage values. I also tried $workbook->setVersion(8); and $worksheet->setInputEncoding('UTF-8');
I am using this tutorial and Google lot for this problem.
http://www.sitepoint.com/article/getting-started-with-pear/3/
Thanks in advance.
I try to use PEAR only when I really need to...you can easily generate an excel spreadsheet( assuming it's just data) with something like this:
$header = "Last Name\tFirst Name\tAge\tJob\n"; // new line is start of new row, tab is next column
//ideally you would get this from a DB and just loop through it and append on
$row1 = "Smith\tBob\t25\tManager\n";
$row2 = "Anderson\tTrent\t32\tCEO\n";
$excel = $header.$row1.$row2;
$xlsfile = "excel_example".date("m-d-Y-hiA").".xls";
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=$xlsfile");
echo $excel;
that's just a tab seperated value file though and you're sending headers suggesting the browser treat that data as an excel file. so there's no formatting of data which the poster may require.
I want to write a text file in the server through Php, and have the client to download that file.
How would i do that?
Essentially the client should be able to download the file from the server.
This is the best way to do it, supposing you don't want the user to see the real URL of the file.
<?php
$filename="download.txt";
header("Content-disposition: attachment;filename=$filename");
readfile($filename);
?>
Additionally, you could protect your files with mod_access.
In addition to the data already posted, there is a header you might want to try.
Its only a suggestion to how its meant to be handled, and the user agent can chose to ignore it, and simply display the file in the window if it knows how:
<?php
header('Content-Type: text/plain'); # its a text file
header('Content-Disposition: attachment'); # hit to trigger external mechanisms instead of inbuilt
See Rfc2183 for more on the Content-Disposition header.
PHP has a number of very simplistic, C-like functions for writing to files. Here is an easy example:
<?php
// first parameter is the filename
//second parameter is the modifier: r=read, w=write, a=append
$handle = fopen("logs/thisFile.txt", "w");
$myContent = "This is my awesome string!";
// actually write the file contents
fwrite($handle, $myContent);
// close the file pointer
fclose($handle);
?>
It's a very basic example, but you can find more references to this sort of operation here:
PHP fopen
If you set the content type to application/octet-stream, the browser will ALWAYS offer file as a download, and will never attempt to display it internally, no matter what type of file it is.
<?php
filename="download.txt";
header("Content-type: application/octet-stream");
header("Content-disposition: attachment;filename=$filename");
// output file content here
?>
Just post a link on the site to http://example.com/textfile.php
And in that PHP file you put the following code:
<?php
header('Content-Type: text/plain');
print "The output text";
?>
That way you can create the content dynamic (from a database)...
Try to Google to oter "Content-Type" if this one is not the one you are looking for.