Error in downloading file PHP - php

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.

Related

Download file using PHP and header - works only with "die" in the end

Can someone explain me what I'm doing wrong?
The code below is working only when I'm adding "die" in the end. Without "die" I'm receiving broken gzip file...
$file = $this->connection->result_body;
$fileName = "test.gz";
header("X-Frame-Options: sameorigin");
header("Cache-Control: no-cache");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.$fileName);
header("Content-Type: application/x-gzip");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. strlen($file));
echo $file;die;

Downloading a pdf file using GuzzleHttp Stream

I have a pdf file read from a file system and right now I can display it to the user directly using echo. This is what I did:
$file = $client->open($storage_root);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . 'sample' . '.pdf"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
echo ($file->getContents());
But I have a requirement to save this file in a temporary location somehow. Any ideas on how I can do that.
Help appreciated!!
I'm assuming you have the contents of the PDF already saved in $file? In that case, you can use:
<?php
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename='" . basename($file) . "'");
readfile ($file);
?>
Hope this helps!

change the name of the downloading file for the user downloading in php or html

Hi Is there is any way which i can give the user different name of the file which i have inside my server
for example i have file inside my server which has md5 name like
33e65007ba9387583d4902e5497fc9ac.mp3
i need when the user click to download this file to change the name of the downloading file to something.mp3
and this changing wanna be just with the user file downloading and it will not effect the name of the file in my server
More detail :
name of server file is :33e65007ba9387583d4902e5497fc9ac.mp3
name of the user file after downloading something.mp3 with
out changing server file
how can i do this thing ? with php ?
You could store file name in $name, and file content in $content. Then, use the following codes to download the file:
header('Content-Description: File Transfer');
header("Content-type: application/force-download");
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $name);
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: ' . strlen($content));
ob_clean();
flush();
echo $content;
i solved the problem by changing my download php file to
<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

Download a pdf's file code in php

$path = BASE_URL."/pdf/";
$filename= $path.basename($_GET['download_file']);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;
filename='.basename($filename));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
exit;
This code works but I'm getting Error in reading pdf file when opening the downloaded pdf. In the above code I get the file from the location http://localhost//eec//pdf/CV_Prabin Mishra.pdf
BASE_URL probably has a slash at the end, so you don't need the extra one:
$path = BASE_URL."pdf/";
I don't see where you ever set the value of $filename, you set the location of the PDF in $fullPath, but then use $filename to read it out. I think the code should be
$path = BASE_URL."/pdf/";
$fullPath = $path.basename($_GET['download_file']);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;
filename='.basename($fullPath));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($fullPath));
readfile($fullPath);
exit;

php - How to force download of a file?

I'm looking to add a "Download this File" function below every video on one of my sites. I need to force the user to download the file, instead of just linking to it, since that begins playing a file in the browser sometimes. The problem is, the video files are stored on a separate server.
Any way I can force the download in PHP?
You could try something like this:
<?php
// Locate.
$file_name = 'file.avi';
$file_url = 'http://www.myremoteserver.com/' . $file_name;
// Configure.
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
// Actual download.
readfile($file_url);
// Finally, just to be sure that remaining script does not output anything.
exit;
I just tested it and it works for me.
Please note that for readfile to be able to read a remote url, you need to have your fopen_wrappers enabled.
Tested download.php file is
function _Download($f_location, $f_name){
$file = uniqid() . '.pdf';
file_put_contents($file,file_get_contents($f_location));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename=' . basename($f_name));
readfile($file);
}
_Download($_GET['file'], "file.pdf");
and the link to download is
Descargar
Try this:
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
The key is the header(). You need to send the header along with the download and it will force the "Save File" dialog in the user's browser.
<?php
$file_name = 'video.flv';
$file_url = 'http://www.myserver.com/secretfilename.flv';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
echo file_get_contents($file_url);
die;
?>
I don't know this is the best way or not but I like that, short & simple.
If you want to download file when you visit URL, you can do like below
<a href="resume.pdf" download></a>
<script>document.querySelector('a').click();</script>
index.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>download</title>
</head>
<body>
download
</body>
</html>
.htaccess
Options -Indexes
Options +FollowSymlinks
deny from all
download.php
$file = "pdf/teste.pdf";
if (file_exists($file)) {
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');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
using this code. is it possible to save the file name to what you want. for example you have url: http://remoteserver.com/file.mp3 instead of "file.mp3" can you use this script to download the file as "newname.mp3"

Categories