Download file php in the server - php

in the localhost i get everything perfect but when i upload it to the server i get this error
Warning: Cannot modify header information - headers already sent by(
hier is my code
<?php
function download($file){
$dir = './download/';
$path = $dir.$file;
if(!file_exists($path)){
die('Error');
}else{
header('Content-Description : File Transfer');
header('Content-Disposition : attachment; filename='.basename($path));
header('Content-Type: application/octet-stream');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
ob_start();
flush();
readfile($path);
exit;
}
}
if (isset($_GET['download'])) {
if (!empty($_GET['download'])) {
$file = $_GET['download'];
download($file);
}
}
?>
<a class="download-template" href="example.php?download=Modern.rar">Download</a>

Remove the whitespace before the <?php
Like so
<?php
function download($file){
$dir = './download/';
$path = $dir.$file;
if(!file_exists($path)){
die('Error');
}else{
header('Content-Description : File Transfer');
header('Content-Disposition : attachment; filename='.basename($path));
header('Content-Type: application/octet-stream');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
ob_start();
flush();
readfile($path);
exit;
}
}
if (isset($_GET['download'])) {
if (!empty($_GET['download'])) {
$file = $_GET['download'];
download($file);
}
}
?>
<a class="download-template" href="example.php?download=Modern.rar">Download</a>
The error is telling you that you're outputting content before it should do.
If you output content then the page headers have already been sent, so your call to header() will fail because the headers have already gone. And headers are always sent first.
By removing the whitespace there is no content to send, so the headers are not sent, so the call to header will then work and not error.

Change the file encoding to "without BOM" (e.g. using notepad++) or remove the BOM before

Use the following code for download any type of file extensions(including .php,.html):
<?php
$filename = $test_data['test_name'];
$contenttype = "application/octet-stream";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
readfile(ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename);
exit();
?>
Or you can check in this link working example:
http://websamplenow.com/29/file_download

Related

PHP downloading files has been corrupted(While downloading from ftp server)

I have problem in downloading file(php)from a particular folder.
when i download and open the file it says your file is corrupted.
when i check the size of the uploaded file and downloaded file it is same , but for zip file size it differs.
No Files are opening.
can any one say where i am wrong???
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
}
else
{
$filename = NULL;
}
$err = 'Sorry, the file you are requesting is unavailable.';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
}
else
{
// define the path to your download folder plus assign the file name
$path = '/public_html/wp-content/uploads/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment;filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file canĀ“t be opened
$file = # fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
exit;
}
inserting into table:
echo "<tr><td><a href='?file=" . $row["FileupName"]. "'>".$row["FileupName"]."</td></tr>";
I am happy that the file is getting downloaded but it not getting opened.
.txt file is getting opened.
Had checked with header also.
i have tried putting:
ob_clean();
flush();
readfile($file);
if (file_exists($path)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
exit;
}

PHP - Forcing download a file not working

code.php
$code = $_GET["code"];
$file = 'code/'.$code.'.html';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
Generated URL to download the file:
http://www.example.com/code.php?code=yoursite.com_nbsp63ibrf
Well, I want to forcing download the html file, above code not working and it just preview the file at browser!
file_exists() returns false. Change your path with document root:
$code = $_GET["code"];
$file = $_SERVER['DOCUMENT_ROOT'] . '/code/' . $code . '.html'; // set your path from document root.
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
Try changing the content type to match the file type and setting the transfer encoding to binary:
header('Content-Transfer-Encoding: binary');
header('Content-Type: text/html');
Try this
$code = $_GET["code"];
$file = $_SERVER['DOCUMENT_ROOT'] . '/code/' . $code . '.html'; // set your path from document root.
if (file_exists($file)) {
header("Content-Type: text/html");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
unfortunately it was a encoding issue. I changed code.php encoding from UTF-8 to UTF-8 Without BOM then problem solved. Thanks for all answers and helps. I forgot that PHP header only works with UTF-8 Without BOM.
This is actually not possible.
The browser can always decide on its own, if the file should be downloaded or not.
The furthest you can go is sending the content-disposition header.

text file download code in php

I am a new PHP programmer. I am trying to write code to allow users to download text file from my website. I followed the answers to similar questions on this subject and put together this following test program. It did not force the download to go to a file, instead, it sent the content to the screen (in Chrome, IE, Firefox). Could someone point out what I did wrong?
Here is my test code:
<?php
$file = "test.txt";
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=\"test.txt\"");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
exit();
?>
You can simply use
$handle = fopen("file.txt", "w");
fwrite($handle, "Shadow ");
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
Edited , try it on single php page .
This code will display the text file content in the screen, and user can download it as a text plain file:
$file = "test.txt";
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
echo file_get_contents($file);
But this code (by #Drop Shadow) will force browser to show download dialog without displaying anything:
$file = "test.txt";
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=\"test.txt\"");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
exit();

How to download image file by force download

I am trying to download image file in CI but getting error when I open download image file. Need help :(
$file_name = $_GET['file_name'];
$file_path = "./ups_printimages/".$file_name;
if(file_exists($file_path))
{
$this->load->helper('download');
$data = file_get_contents($file_path); // Read the file's contents
$name = 'ups_label.png';
force_download($name, $data);
}
else
{
echo "file does not exist";
}
GOD. Found out the solution :)
if(!file)
{
File doesn't exist, output error
die('file not found');
}
else
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($file);
exit;
}
Use header to force download. Use the code below
$file_name = $_GET['file_name'];
$file_path = "./ups_printimages/".$file_name;
if(file_exists($file_path))
{
header('Content-Type: image/jpeg');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_name) . "\"");
readfile($file_path);
}
else
{
echo "file does not exist";
}
Hope this helps yoiu

How to download a file from specific directory using php header

I have some code to download file using php header but it is not properly working and want to add directory to read
<?php
if(isset($_GET['link'])){
$var_1 = $_GET['link'];
$dir='/upload/';
}
?>
<?php
if(isset($_GET['link'])){
$var_1 = $_GET['link'];
$file = $var_1;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
}
?>
It shows error
Content error
The file does not exist!
I am Using
http://sap.layyah.info/download.php?link=UAC.dll
this link to download the file file original location is
http://sap.layyah.info/upload/UAC.dll
First, the quotes in $file = '$var_1'; won't get interpreted correctly,
therefore it needs to read as $file = $var_1;
You also have a missing closing brace }
<?php
if(isset($_GET['link']))
{
$var_1 = $_GET['link'];
$file = $var_1;
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
} //- the missing closing brace
?>
And you mentioned that you wanted to use a different folder.
You could use something to the effect of:
$dir = "folder/"; // trailing slash is important
$file = $dir . $var_1;
or
$dir = "../folder/"; // trailing slash is important
$file = $dir . $var_1;
depending on the folder's location.
Edit
The following is tested and worked for me and the files were run from the root of my server.
<?php
if(isset($_GET['link']))
{
$var_1 = $_GET['link'];
// $file = $var_1;
$dir = "folder/"; // trailing slash is important
$file = $dir . $var_1;
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
} //- the missing closing brace
?>
HTML (I used a PDF file as an example)
Download here

Categories