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");
Related
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.
This question already has answers here:
Forcing to download a file using PHP
(10 answers)
Closed 9 years ago.
When I am downloading an image it's displaying these characters on screen.
�M\cIDATx��\i��v�U�ݳ��x���6`8�Y�&#��#D �����HDʟ'�~�z;a���D,�c������ƌ���ٺ�*�w�=շjz^z�ـ���UwWWݺ�;�|g��&H���o�Gy���~s��K۲e�y�7�ѣG�̙3ͤI������3�O=�T���n�z�)�k�.�p�B��NJ��6m���a(�ٳgOt��i���|����hhh((�˦����{����3��sO�C�:�_�k�������s�)�|X�V��_C�6���2o~G���3����y۶m��o4�\s�) r�ܹse.|-[�,�`~/N� GQdU(h!Z� |E���J��h�&�u�sާ�ES�T���æ��ׯh�o�>���ˡ�ˠ�-��-m�{ �\p�#Mou�֙���{�qN;,�ŋ�cߏ䦦&3u�T���)��MS�yrP���B|o���Ԏ�l1�C&L8�����j�j/_4ϯ���4��o�o��w�>|8�5���M�P�;f���͆ Ҿ&N�hJM%C�,Z�� Z=�9螞�=~��4�7H�������#���-"0�jWW�1���{[[[�:;'�Θ1�0�+�^h�h�>��#�Aw�;�� ��Ba�J%�ޓ���;�����с� ���4�2�<xͩt�8��="" p�ٴi�����="" 4g�q8��:��u�9q:v�i��x(���r�b��m��n��f�ml���="" ���)��n�="" ="" -��="" ���o����+�itj�_⧟~"Ȣ="" Ԯj��`�!�x��sn��n�g��'�j��cmv��o="" !�����?������r���p��(��)�,Ԭ^�z�0k֬���ŀh�`�5�'��1�����&\��+�2�o="" �v��4�="" �ac="" ��="" 4�f+�e�="" �ӭ�w����j���q�#�)��t̟??��x4�7e���oh�6o���r&�lh�z�n��l�8��pn���kἎbhj�e�-�|!�*���ɪ��z="">�E��Ŵ��;���Ʃ�0����j\�]o^�X�A�qο�8cVh�Q�M�x��F](L3�#'f�T�*4�IxZ,K8��͜ ! ���S��MJ�h� 5��2��p�!��wۇ�n� �M�/Z�c�=&Q��_h&�8� �X8��.��鑪&uRL���b�j�~Wg���A �d#MUG(�+�B_r�$�h�w ���i 2���ʠ>�QƸ�v�e�n�~|�fm�1� D��6K�w{����z��7T0�����}���ĩ#��Q�8K�Q�"�8�^2��d�N�+l�$j3�j����h'�x�V.��qmA�����P�?[� ^bIFE�Q����#�{i���o��� �:�<&��Y���Ѳ%�L�U��܍�����ź�ZB�\���*N��X� G!*=w�J#-���k5� m��\ 7�8O~��,��=�݄}Jp�?�P�L)�P��j4F�����"Ds:��I�o���^{M����*4H#�
And I used this code for downloading
$fn = $path.'/'.$file_name;
$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($fn)) );
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary\n");
readfile($fn);
I have tried with your code and found no problem. After read your comment and try with your file thetexturemill.com/wp-content/uploads/2013/07/dell.png I have this code working:
# my demo value in my local machine
$path = dirname(__FILE__) . "/demo";
$file_name = "Capture.PNG";
#$fn = realpath($path.'/'.$file_name);
$fn = "http://thetexturemill.com/wp-content/uploads/2013/07/dell.png";
//var_dump(readfile($fn));
$mm_type="application/octet-stream";
#$mm_type=mime_content_type($fn);
#echo $mm_type; die();
ob_get_flush();
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
#header("Content-Length: " .(string)(filesize($fn)) );
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary\n");
readfile($fn);
flush();
die();
What problems I found are:
If you use an image from remote host, make sure you can get it (the allow_url_fopen INI option is ON and the returned value from readfile is greater than zero) and do not use filesize as well as mime_content_type functions.
I don't know whether thetexturemill.com is your domain name or folder name. Supposed that it is a domain name, remember to add the protocal prefix (http:// as in example)
Do not output anything before the header function calls or your downloaded file will not be open properly.
Ah, for local file, your original code work without errors on my machine.
The content type is wrong
application/octet-stream
Ocet-stream is used for executable files which images are not for sure.
A proper type for a image for jpg image is for example:
image/jpeg
You can use mime_content_type() to get proper content type of file
Returns the MIME content type for a file as determined by using information from the magic.mime file.
Try this code
$fn = $path.'/'.$file_name;
$mime = mime_content_type($fn);
header('Content-Type:'.$mime);
header('Content-Length: ' . filesize($fn));
readfile($fn);
Try this,
<?php
$fn = $path.'/'.$file_name;
$mm_type="application/octet-stream";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Length: " .(string)(filesize($fn)) );
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Disposition: attachment; filename="'.$fn.'"');
header("Content-Transfer-Encoding: binary\n");
ob_clean();
flush();
readfile($fn);
exit;
?>
Read this http://php.net/manual/en/function.readfile.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.
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"