below is the php code I am using,
<?php
if(isset($_POST['tarea'])){
$filename = $_SESSION['Zone'].'.zone';
$data = $_POST['tarea'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
echo $data;
}
?>
I am just trying to download content of text area as downloadable file but it is outputting html code too in the file, I have tried to use die($data); instead of echo $data; but it does not work.
Please help me to solve this problem out. Thanks.
I hope you can resolve your problem by using
$data = strip_tags($_POST['tarea']);
instead of
$data = $_POST['tarea'];
If this will not help you then use
ob_end_clean();
before using header() function.
I created two files, tarea.php (for the form) and download.php
tarea.php
<?php
session_start();
$_SESSION['Zone'] = 'test';
?>
<form action="download.php" method="post">
<textarea name="tarea"></textarea>
<input type="submit">
</form>
download.php
<?php
session_start();
if(isset($_POST['tarea'])){
$filename = $_SESSION['Zone'].'.zone';
$data = $_POST['tarea'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
echo $data;
exit;
}
?>
Related
I would like to make a simple HTML file generator with PHP, where user inputs some data, and the .php file generates an output with HTML code, including the data from user.
The problem is that my generator generates only a blank .html file.
The form is simple:
<form action="html_form_submit.php" method="post">
<textarea name="name" rows="2" cols="20"> </textarea>
<input type="submit" value="Submit"/></form>
And the html_form_submit.php file:
<?php
ob_start();
$name = #$_POST['name'];
?>
<html><body>
Name: <?php echo $name; ?><br>
</body></html>
<?php
$output = ob_get_contents();
$filename = 'test.html';
!$handle = fopen($filename, 'w');
fwrite($handle, $output);
fclose($handle);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
ob_end_clean();
?>
Do you know, where can the problem be?
Try this
at html_form_submit.php file:
<?php
ob_start();
if(isset($_POST['name'])){
$name = $_POST['name'];
}
?>
<html><body>
Name: <?php echo $name; ?><br>
</body></html>
<?php
$output = ob_get_contents();
$filename = 'test.html';
!$handle = fopen($filename, 'w');
fwrite($handle, $output);
fclose($handle);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
ob_end_clean();
?>
You should move invocation of ob_end_clean immediately after the $output = ob_get_contents();.
ob_end_clean clears your buffer so the response comes back empty.
Try like this:
<?php
ob_start();
$name = #$_POST['name'];
?>
<html><body>
Name: <?php echo $name; ?><br>
</body></html>
<?php
$output = ob_get_contents();
ob_end_clean();
$filename = 'test.html';
!$handle = fopen($filename, 'w');
fwrite($handle, $output);
fclose($handle);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
I have a link which shows the filename to download.When a user clicks it,it needs to get downloaded.The file gets downloaded but it contains only 0 KB.In console it shows
Resource interpreted as Document but transferred with MIME type application/force-download: "../download.php?file=filename"
My code is like this:
<a href="download.php?file=user_uploads/'.$_path['uploads'].
'logo_images/'.$row['FileName'].'" title="Click to download">'.$row['FileName'].'</a>
The download.php is like this:
<?php
$path = str_replace('/download.php?file=','',$_SERVER['REQUEST_URI']);
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . basename($path . $uri[1]) . "\"" );
#readfile($path);
?>
Thanks in advance.I have checked the path of the file also.
try
Click
download.php
<?php
$path = 'yourpath'.$_GET['file'];
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$_GET['file'] );
#readfile($path);
?>
file.txt - change with your file name
I had similar issue while downloading my file. I used this code for download.php:
<?php
$path = $_REQUEST['path'];
#setting headers
header('Content-Description: File Transfer');
header('Cache-Control: public');
header('Content-Type: application/zip');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename='. basename($path));
header('Content-Length: '.filesize($path));
ob_clean(); #THIS!
flush();
readfile($path);
exit;
?>
and my link was:
Download Pack
Hope it helps.
I have the following PHP code using headers to download a file from the server:
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
This code works fine, however I have it in a file with the HTML form and it is ran using an if isset all of the HTML code is placed in the file, as well as anything I echo out in PHP; and the data I actually want in the file is there at the end.
I can't figure out what in the headers is causing it to write everything that is on screen to the file when downloaded. The file on he server isn't changed and is how it should be.
Thanks for any help.
Heres all the code...
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><br>
Order Number: <input type="number" name="orderNo" size="10"/>
<input type="submit" name="Download" value="Download" />
</form>
</body>
</html>
<?php
if(isset($_POST['Download'])){download();}
function download()
{
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('amazondb', $conn);
$result = mysql_query("SELECT ID, PurchaseDate, BuyerName, ShipCity, ShipState, ShipPostalCode, ShipCountry,
ShipAddress1, ShipAddress2, ShipAddress3 FROM imported_orders");
$orderNo = $_POST['orderNo'];
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if($orderNo>0&&$orderNo<=count($row))
{
$file = fopen('Order.txt', 'w');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if($row['ID']==($orderNo))
{
echo "Hello";
fwrite($file, $row['BuyerName'].PHP_EOL .$row['ShipAddress1'].PHP_EOL .$row['ShipAddress2'].PHP_EOL .$row['ShipAddress3'].PHP_EOL .$row['ShipCity'].PHP_EOL .$row['ShipState'].PHP_EOL .$row['ShipPostalCode'].PHP_EOL .$row['ShipCountry'].PHP_EOL);
}
}
fclose($file);
$file = 'Order.txt';
header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
}
else{echo "Please enter a valid Order Number"; echo $orderNo;}
}
move this code at very begin of file, so you will have:
<?php
if (your condition to output file) {
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
die();
}
the rest of your file....
another solution, at the very top of file add:
<?php
ob_start() or die('Cannot start output buffering');
... your page
and just before the first header add:
$file = 'Order.txt';
ob_end_clean();
header("Cache-Control: public");
...
readfile($file);
die();
So I have been trying to get upload/download system to work but kept running into problems, specifically, the downloaded file being corrupt (.xls).
This is what I have currently:
<form action="{{block type=" core/template" name="my-template"
template="php/download_file.php" }}" method="post">
<label for="date">Date:</label>
<input type="date" name="date" id="date"><br>
<input type="submit" name="submit" value="Download">
</form>
download file
So, these both link to the same file and downloads fine.
If I click on the download file link, file opens perfectly fine.
If I go through the form download button, then it'll download but opening gives me a warning/error: "the file format and extension of 'file' don't match" and just hangs forcing me to force close the file.
download_file.php:
<?php
if($_POST['submit']) {
$file = 'excel_file.xls';
// Magento file path
$path = Mage::getBaseUrl('media') . 'folder' . DS . $file;
header("Content-Type: application/vnd.ms-excel");
//header("Content-Type: application/octet-stream");
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename = 'filename'");
echo $path;
exit;
}
?>
This is all in a Magento Static Block, in case that has anything to do with it.
Any help is appreciated.
Thanks.
Have you tried this:
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xls"); //File name extension was wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
Greetings.
Try changing:
header("Content-Disposition: attachment; filename = 'filename'");
to:
header("Content-Disposition: attachment; filename=filename.xls");
Or if you use a variable:
header("Content-Disposition: attachment; filename=$filename.xls");
Basically, start and end with a quote and don't use any quotes or apostrophes in between.
I have a file, index2.php that has been written to by a form.
The whole content of this file, I have stored within a variable $final_code using an output buffer.
I now wish to add a "Download" button to the end of the page, that brings up a Save As dialog, allowing the user to save this file's source code (i.e. the variable) as a .txt - But I'm stumped.
index2.php:
<?php
// Start buffering the output
ob_start();
?>
<!-- INDEX2.PHP HTML ELEMENTS HERE -->
<?php
// Store the contents of the buffer
$final_code = ob_get_contents();
// Print the contents of the buffer
ob_end_flush();
?>
<form action="savefile.php" method="post">
Happy? Save this to file: <input type="submit" name="savefile" value="Save" />
</form>
I'm not sure if this needs to be worked in to this file or savefile.php, so the latter is currently blank.
i think you cannot force a save as file dialog in browser.
for forceing the download of a txt file i do the following:
header('Pragma: anytextexeptno-cache', true);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"example.txt\"");
echo $output;
hope that helps.
You need to use headers to force download:
$file = 'somefile.zip';
if(!file)
{
// File doesn't exist, output error
die('file not found');
}
else
{
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}
Example taken from:
http://www.ryboe.com/tutorials/php-headers-force-download
See link for further explanation.
Are you looking for PHP Download Script(from browser) ?