How to force-downloads in PHP? - php

How do I force-download for multiple links? If user clicks link1 they download pdf1.pdf. If user clicks link2 they download pdf2.pdf. Is there a way to check which link they clicked on?

Try
<?php
$FileName = 'file.pdf';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);

$file_url = 'your_file.someting';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
P.S. use proper content-type for your file e.g application/zip for ZIP files

Related

Save to directory box

I have this php code that helps user to download a file from database. But the downloadable file is downloaded directly into default computer download folder. What I want is to popup a box to ask the user where to save the file, so if their any help. Thank You.
<?php
$data = $_REQUEST['data'];
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
//header("Content-disposition: attachment; filename=\"" . basename($data) . "\"");
header("Content-disposition: attachment; filename=\"" . basename($data) . "\"");
readfile($data);
?>

Error in downloading file 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.

How to hide a download link

I have a PHP script that is supposed to allow the users to download a file without showing the URL. When I try out the following, the file I download is empty. Could someone please help?
<?php
$file_name = 'test.exe';
$file_url = 'https://www.example.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-disposition: attachment; filename=".$file_name);
readfile($file_url);
?>
you should use like this
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary")
header("Content-disposition: attachment; filename=\"file.exe\"");
echo readfile($url);
also content type based on your file application/zip, application/pdf etc
AND or better one for exe
header("Location: $url");

How to download PDF file from blob oracle using PHP?

I want to download blob file from oracle, that is PDF file, this is my code to get and download file:
<?php
$conn = ocilogon('user', 'pass', '//localhost/XE');
$sql = "SELECT PDFFILE FROM TFILE";
$stid = ociparse($conn,$sql);
ociexecute($stid);
$rowResult = ocifetch($stid);
settype($arrayResult,"array");
if($rowResult != null){
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . OCIResult($stid,'PDFFILE') . '"');
header("Content-Length: " . filesize(OCIResult($stid,'PDFFILE')->load()));
header('Content-Disposition: attachment; header("Content-Transfer-Encoding: binary\n");
}
?>
but when i run this code,i not get pdf file..
something wrong with my code??
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . OCIResult($stid,'PDFFILE') . '"');
header("Content-Length: " . filesize(OCIResult($stid,'PDFFILE')->load()));
header('Content-Disposition: attachment; header("Content-Transfer-Encoding: binary\n");
You're sending the Content-Disposition header twice. You probably don't even need the second one, the client should know all it needs to know about the stream from the Content-Type header. Omit the second Content-Disposition so you're not over-writing the header that has the filename.

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