I read blob (word 2010 document) from mysql database and store it in $data variable. When I simply store that data directly in PHP like so:
file_put_contents('c:\\temp\\dump.docx', $data);
I can open dump.docx in Word (size matches original file). If I attempt to send $data like this:
ob_start();
header('Content-disposition: attachment; filename=' . $name);
header('Content-type: ' . $type);
ob_clean();
echo $data;
ob_end_flush();
exit;
The stored file is two bytes longer. There are two spaces in front:
To check if I somehow do not output those spaces, I called ob_get_contents() just before echo and dumped content to a file. File has zero bytes.
So it looks like echo is producing those two bytes.
Here's post that helped me:
https://drupal.stackexchange.com/questions/163628/extra-space-at-beginning-of-downloaded-image/163644
ob_start was already called ealier. I needed to call only ob_clean() before sending content.
Related
So I'm trying to export a csv using PHP in which the contents contains UTF-8 character and I want the resultant csv to open in Excel smoothly (including Mac excel)
So there is an answer here: How can I output a UTF-8 CSV in PHP that Excel will read properly?
Checkout the top answer.
But then in order to implement that you need to use tabs to separate the fields instead of commas...Is there a way to achieve this while still using commas and not tabs and still have it work in OS X
EDIT
Mostly to Mark Baker but everyone feel free to comment
Another code update
while(#ob_end_clean());
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=fileexport.csv");
echo "\xEF\xBB\xBF";
print "sep=,\n";
print $output;
exit;
fputcsv should work fine in this instance. Take the following example, where as the third parameter of fputcsv is the delimiter. By default it is , (comma), but you could also use "\t" for tab files. CSV files should be interpreted the same on either OS
if( $fh = fopen("output_file.csv","w") ){
$put = array("column1, with comma","column2, with comma","column3" /*,"columnN"*/);
fputcsv($fh,$put,",");
fclose($fh);
}
i am using php code to export data to CSV file.Everything is working fine as required.but problem is that when there comes long text in a cell.I want to wrap text so that i can increase cell size to handle long text.Below is my code.
header("Content-Type: application/force-download\n");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Content-Disposition: attachment; filename=store_earning_report.csv");
echo "Notes \n";
echo $Notes;
echo "\n";
exit;
I have searched but didn't find any solution.Is there any way to handle this problem.
Thank you.
Make sure you are including a comma "," after each field, and "\r\n" to trigger a new line in the .csv file that is created.
A .csv is just a text file with commas used to separate the field values - So there is no way you can control the cell sizes that will appear when the file is first opened in Excel.
I have the following URL:
http://www.solutionssoftwarematrix.com/download.php?filename=CreatingandMarketingthePerfectYouTubeVideo.zip
I am trying to pass the string variable in to the PHP script (below) when someone clicks the link above.
Here is the script (download.php):
http://www.devshed.com/c/a/PHP/Simple-and-Secure-PHP-Download-Script-with-Limits-Tutorial/1/
THE LINES BELOW with BOLD AREAS are where my $_GET FUNCTION is BEING ADDED, however, I keep receiving parse errors OR it does not pull the filename, and the download does not work.
header("Content-type:application/pdf");
header('Content-Disposition: attachment;filename="http://www.yourdomain.com/ebookfordownloads/$_GET['$filename']"');
readfile("/your/absolute/server/path/html/ebookfordownloads/$_GET['$filename']");
header("Content-type:application/pdf");
header('Content-Disposition: attachment; filename="http://www.yourdomain.com/ebookfordownloads/$_GET['$filename']"');
readfile("/your/absolute/server/path/html/ebookfordownloads/$_GET['$filename']");
There are multiple problems:
This syntax is invalid: '$_GET['$filename ... The apostrophe before '$filename closes the string. What you probably want to do is downloads/' . $_GET[$filename] -- concatenation
This syntax is also invalid: "downloads/$_GET['$filename" -- with array access in a quoted string, you cannot have an apostrophe. Again, concatenation is your best bet: "downloads/" . $_GET[$filename]
I'm not sure whether you want $_GET['$filename'] or $_GET[$filename] or $_GET['filename']. There is a very important difference between the three.
Your code has a large vulnerability if a user can set $filename somehow (or the filename get parameter, if that is what you intend to use).
You are not getting basic PHP string functionality. To add variables like that, you'll have to use this:
readfile("/your/absolute/server/path/html/ebookfordownloads/{$_GET['$filename']}");
or concat them:
readfile("/your/absolute/server/path/html/ebookfordownloads/".$_GET['$filename']);
Not to mention the Very Big Security Hole when using user input ($_GET variables) verbatim in your code. Do validation before you use the given information.
Suppose this is how it should be:
header("Content-type:application/pdf");
header('Content-Disposition: attachment;filename="http://www.yourdomain.com/ebookfordownloads/' . $_GET['filename'] . '"');
readfile("/your/absolute/server/path/html/ebookfordownloads/" . $_GET['filename']);
header("Content-type:application/pdf");
header('Content-Disposition: attachment; filename="http://www.yourdomain.com/ebookfordownloads/' . $_GET['filename'] . '"');
readfile("/your/absolute/server/path/html/ebookfordownloads/" . $_GET['filename'] );
Take a look at string concatenation in PHP.
Also: $_GET['$filename'] means that you are asking PHP to get everything after ?$filename= in url. But your url has no $filename parameter, only filename, so it should be modified to $_GET['filename']. Or, if you have a name of parameter stored in variable called $filename like this: $filename = 'filename', it should be changed to $_GET[$filename]
I've created a script which takes names from my db and for each name creates a .php file for them. However I would like to fill this file already with a (php) header since letters such as ë é and ú are not shown correctly without it.
So far i've got this:
<?php
// ...
while($row = mysql_fetch_array($res)){
$file = "Map/" . $row['name'] . ".php";
$fh = fopen("$file", "w+");
if($fh==false){
die("unable to create file");
}
echo "$file created <br/>";
$stringData = "<center><h1>". $row['name'] . "</h1></center>\n
No text available just yet.";
fwrite($fh, $stringData);
}
fclose($fh);
?>
What i would like to do now is place the following line of code even before the center tag:
<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>
As i retrieve the stored text from the .php file using an ajax request (with JQuery), the header must be in the file in order to be able to show those special letters. Thus my question: how can i place this line of code in each file, rather than php just executing it? (Or if this can be done easier, how so?)
Edit after comment:
After the script has run i would like to have a file that looks like this:
<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>
<center><h1>Chocolate</h1></center>
No text available yet.
You have to use meta tag in head section of your HTML, it tells the browser which character encoding is used:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15"/>
By the way, if you're starting a new project, think about using the UTF-8 encoding. It often is a better choise than ISO-* because it allows all Unicode characters (all characters of the whole world) with one encoding.
Try adding this, immediately after opening the file:
fwrite($fh, "<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>\n");
PHP is smart enough to know that ?> is not an end-PHP marker when it appears in a string literal.
Please ask about your real question; not about what you assume could be the solution. As far as I understood it the question should read something along: "How to set a specific file encoding via PHP?"
If your output is an HTML file use a META-tag as suggested by Glibnes.
If the written file is plain text, you should convert your text to UTF-8 and fwrite() it to the file.
Use the following stringData instead of the above
$stringData = '<?php header(\'Content-Type: text/html; charset=ISO-8859-15\'); ?><center><h1>'. $row['name'] . "</h1></center>\n
No text available just yet.";
As you are writing it in .php file. It should execute the header function.
I am looking for a simple way to take an array, turn it into a CSV and have a user be able to download the CSV, all without saving the CSV to the server.
$array = [
['name', 'email'],
['Doe, John', 'johndoe#foo'],
['Jane Doe', 'janedoe#foo'],
['Ron "SuperFly" O\'Neal', 'supafly#foo'],
];
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=test.csv");
$fp = fopen('php://output', 'w'); // or use php://stdout
foreach ($array as $row) {
fputcsv($fp, $row);
}
see fputcsv()
The benefit of using fputcsv is it will handle escaping and enclosing of fields for you, ensuring valid, rugged output that will never break even if your columns values happen to contain csv control characters such as commas, quotes, newline characters etc... If you don't take care to handle control characters robustly, and instead take a common shortcut such as using implode(',', $row) your (low quality) code will produce broken csv output.
Unfortunately, fputcsv cant output to a string, only to a stream, so I write to php's output stream via php://output which is equivalent to echo'ing it out. Note that just like output via echo, it will go through php's output buffer mechanism, and that includes any possible output handlers, such as ob_gzhandler or customer output buffer callback functions that may modify the output. If you wish to bypass the output buffer, you can instead write directly to php://stdout.
You could also write to php://memory, php://temp, or a file if you need to access the csv file contents as a string before outputting it.
$fp = fopen('php://memory', 'w+'); // or php://temp
foreach ($array as $row) {
fputcsv($fp, $row);
}
// Read it back into a string, if desired.
// Rewind the stream to start.
fseek($fp, 0);
// Read the entire stream back to string.
$csvContents = stream_get_contents($fp);
info on stream wrappers http://www.php.net/manual/en/wrappers.php.php
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=test.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "name,city,street\n";
Sometimes it's better to use
header("Content-Type: application/octet-stream");
for IE...