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);
?>
Related
I have to start a download with php of a file inside a password protected folder.
I have the following code:
<?php
$File="https://testuser:password#www.testdomain.com/test/2/file.zip";
if(file_exists($File))
{
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");
readfile($File);
}
?>
file_exists and filesize seems that don't work with this kind of url.
How can I solve the problem?
i am trying to download a .pdf file on button submit, following is my code
if(mail('myemail#gmail.com', 'Brochure Downloaded ', $string)){
$text= 'Your message recieved, We will contact you shrtly ';
$file_url = 'http://www.website.com/brochure.pdf';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url); // do the double-download-dance (dirty but worky)
}
Now whats happening is that email is sent but files is not downloaded, instead page gets reloading and long characters are printed on the screen ,
Need your help with this please
this is what i get
I have changed headers a little bit. I think the proper content type is application/pdf andsome of headers are not necessery.
if(mail('myemail#gmail.com', 'Brochure Downloaded ', $string)){
$text= 'Your message recieved, We will contact you shrtly ';
$file_url = 'http://www.website.com/brochure.pdf';
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='" . basename($file_url) . "'");
readfile($file_url); // do the double-download-dance (dirty but worky)
}
You may also add the content length:
header('Content-Length: ' . filesize($file_url));
check this code, first you need to add content type if you download any type file as like if you want to download image then
header('Content-Type: image/png'); if pdf then
header("Content-Type: application/pdf"); etc
<?php
if (mail('myemail#gmail.com', 'Brochure Downloaded ', $string)) {
$text = 'Your message recieved, We will contact you shrtly ';
$file_url = 'http://www.website.com/brochure.pdf';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_url");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
readfile($file);
}
?>
you may use
header('Content-Length: ' . filesize($file_url));
you should use Content-Length if you want someone download a file with another header
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
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 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.