form vs href link corrupt - php

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.

Related

PHP - Generate file on the fly for android devices

I have read some answers here but i cannot seem to make my php script to work.
I generate a text or csv file (it depends what the user has chosen) from a form that it is submitted back to the same page.
The script works fine on Chrome, IE, Mozilla on my desktop but when i try on the stock browser on Android i get an attachment.html file that has the source of my script.
If i try with NEXT browser, i get a file with the proper filename but again inside is the source of my script.
I have read the following link but i cannot make it work
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
My http headers are the following
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($FileName) . "\"");
header("Content-Length: " . filesize($FileName));
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Connection: close");
print $Content;
exit();
-----Update 1----
I wrote the following code to test things out
<?php
$get = $_GET['get'];
if ($get == 1) {
$content = 'This is a line of text!';
$filename = 'superfile';
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($filename) . ".TXT\"");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Connection: close");
echo $content;
exit;
}
echo '<html>
<head>
<title>Test</title>
</head>
<body>
<form method="get" action="index.php"
<label>Get</label>
<input type="text" name="get"/>
<input type="submit" name="ok" value="Send"/>
</form>
</body>
</html>';
?>
When i send the form with get method everything works, when i use post i get the html source that you see, same as the problem that i have with my original script.
-----Update 2-----
Okkkkk, now some more info, i think it is related to an Android bug, please check the following link
http://roscopeco.com/2013/04/android-and-the-form-post-download-problem/
Waiting for suggestions to work around this issue, i need method to me post because i pass too many varialbes and i don't want to have on big ugly url on the address bar.
Well, i didn't want to experiment too much so i get the user-agent and i change my post form to a get. Not a great fix but the job is done.

PHP script that does not reveal real path generates download with ".php" extension

A file called "transfer.php" should offer a file for download. Instead of the download, it offers a file with a ".php" of the same file size as the original file. Did I get the header wrong?
Thanks!
<?php
session_start();
$path = $_SESSION['myvar'];
$mm_type = "application/octet-stream";
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: ".$mm_type);
header("Content-Length: ".(string)(filesize($path)) );
header("Content-Disposition: attachment; filename=".basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
While I originally suggested removing the trailing unpaired quote in the disposition header ..
The file name should be enclosed in quotes (thanks Wrikken!). In the posted code the first quote is missing. Using ' for the first string makes the pairing (or lack of) more clear:
header('Content-Disposition: attachment; filename="' . basename($path) . '"');

PHP textarea content download not working properly

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;
}
?>

Bring up Save As dialog within PHP

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) ?

Download Image link using php

I downloaded this code to use as a download button.
<?
$filename = $_GET["filename"];
$buffer = file_get_contents($filename);
/* Force download dialog... */
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
/* Don't allow caching... */
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
/* Set data type, size and filename */
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($buffer));
header("Content-Disposition: attachment; filename=$filename");
/* Send our file... */
echo $buffer;
?>
The thing is, the name of the file ends up with the whole path in the file name, for example, this code:
<a href="download.php?filename=images/something.jpg">
Ends up with an image named "images_something.jpg"
I'd like to remove the "images_" from the final file name, so far I haven't had any luck.
Thanks for the help!
If you need the file name part without folder name, you have to use basename($filename)
http://php.net/manual/en/function.basename.php
basename()
$filename = basename($path);
p.s
Setting Content-Type several times may not be the best way to force a download. Also, I hope you're sanitizing that $filename argument before you use a file_get_contents.
p.p.s
Use readfile, don't cache it in the memory.
$filename = basename($filename);
header("Content-Disposition: attachment; filename=$filename");
Set your filename to only be the basename?
Don't do it at the top unless you change the variables though so your pathing to it still works.

Categories