How To Download File TO Desired FOLDER/Path? - php

I am having a problem with how to download the file into our desired folder, this below is my code for download.php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fileName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header ("Cache-Control: must-revalidate");
header('Pragma: public');
ob_clean();
flush();
readfile($data);
echo $data;
exit;
I want to download the file to my desired folder like C:\Users\Asus or D:\Program not to the default one
$data is the content of the file and $filename already include the extension Ex:picture.jpg
Thx before for all the help that i can get :)
Henry

There is no way to tell the client where on the client's disk to save a file to.
You have no way of knowing (on the WWW at least) what directories exist
If the user doesn't pay attention to where something is being saved (because they expect it to be the default) then you could save the file somewhere they wouldn't want it (such as a Startup folder) which would be a security risk.

The download directory is usually handled by the browser, not by the server or PHP it's self :)

Related

PHP Downloads Some Empty Files and Some Normally

I have this code to download a Zip file from the server:
<?php
$path_parts = pathinfo($_GET['a']);
$file_name = $path_parts['basename'];
$file_path = 'temp/' . $file_name;
if (file_exists($file_path)) {
$size = filesize($file_path);
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
header('Expires: 0');
header("Content-Type: application/zip");
header("Content-length: $size");
header("Content-Transfer-Encoding: binary");
// read the file from disk
readfile($file_path);
}
else
echo 'File does not exists';
?>
The files are stored in a /temp folder and the curious thing is that if I download a TXT file, it downlads OK, but if the file is a ZIP file then it downloads an empty file. Even if I change the extension of the ZIP file to TXT it still downloads empty, but any other file download just fine. What could be causing this behavior?
In fact, it downloads any other file but the ZIP files in the folder.
It may be the file size, not the extension. Look in your php.ini file and see what these values are set to. If your file is larger than these, this would explain it.
post_max_size=5M
upload_max_filesize=5M
The problem is that PHP is not reading the ZIP files generated by the Java App that produces them. But, any other ZIP file is being read.
As I am using Java to create the ZIP files, I had to replace the method by this one posted here (https://www.mkyong.com/java/how-to-compress-files-in-zip-format/). It now works normally.
Try to use
header("Content-type: application/octet-stream");
instead of
header("Content-Type: application/zip");

Force download on a ftp:// link

I have a PHP that scans the files in a remote NAS Hard Disk via FTP protocol, generates a json file and then via javascript I list those files in the browser.
When the user click a link to a mp4, jpg and many browser-known formats, the browser opens te content instead of downloading it.
Now, I know that with PHP or .htaccess I can change the headers to force the browser to download the file but the file is in a remote location and can only be access via FTP so I can't run PHP or .htaccess in it.
I tried this header variations in PHP:
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"$file\"");
or
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file\"");
or
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/image");
header("Content-Transfer-Encoding: binary");
all ending with:
header("Location: $url");
(url being ftp://user:password#dyndns.org/folder/file.mp4)
but it always opens the file in the browser instead of downloading (on recognized file extensions of course)
Any ideas? Thanks
The previous headers may not work when using a redirect. Instead, you better serve them via PHP instead of redirecting:
header("Content-type: octet/stream");
header("Content-disposition: attachment; filename=".$file);
header("Content-Length: ".filesize($file));
readfile($file);
But note, that this WILL use your own bandwidth. However, there isn't any way to force a behavior on a remote host.

Force browsers to download a file rather than open

I'm using typical PHP code to download documents:
header('Content-Type: ' . $mimeTypes[$fileext]);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-Control: private');
header('Pragma: private');
readfile($filepath);
Everything works fine - a download/save-as dialog opens, except for .doc files which attempt to open in docs.google.com but they fail due to lack of permission - that's because I'm serving files from outside a website root.
So how do I bypass docs.google and force every browser to offer save-as dialog regardless of the file mime type? ('doc' => 'application/msword')
I tried the following to no avail:
in .htaccess file:
<FilesMatch "\.(?i:doc)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
in .htaccess file:
AddType application/octet-stream .doc
in PHP script:
header('Content-Type: application/force-download');
Add this to your headers:
header("Content-type: application/force-download");
The following php worked for me
header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$file."");
header("Content-Transfer-Encoding: binary");
header("Content-Type: binary/octet-stream");
readfile ($link);
P.S. - I used the File extension in the $file variable only, so no need to mention mime type...
Hope this helps :)

Downloaded filename of file using X-Sendfile

Im using X-Sendfile to send a file instead of readfile. The script processing this, is named download.php and contains this:
$video_file = '/path/to/file/file.mp4';
header('X-Sendfile: '.$video_file);
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; file=\"file.mp4\"');
exit();
But problem is that the downloaded file is always named "download.php" (155Mb), and i would like to download it named as file.mp4. Ive tried several things like:
header('Content-Disposition: attachment; file="file.mp4"');
header('Content-Disposition: attachment; file=file.mp4');
And all other posibilities, but still downloading the file as download.php.
My htaccess file contains: XSendFile On
You are sending an incorrect header. The filename property is called filename, not name:
header('Content-Disposition: attachment; filename="file.mp4"');
See RFC2616 for a detailed description of the header.

Generate download file link in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
open download dialog with php
I have a link in my page say, <a href='test.pdf'>(Test.pdf)</a>.
When I click on that link, download dialogue box should open to download that file.
Can anyone help me in implementing this in PHP?
thanks
$filename = 'Test.pdf'; // of course find the exact filename....
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
Name the above file as download.php
HTML:
Test.pdf
That should do it.
test.pdf
In the case of a PDF file, most browsers are going to look for the helper (acrobat) to load it in your browser by default. You are trying to get around this default behavior is my guess.
The easiest way to do this (assuming you're on *nix box with apache) is to make an .htaccess file in the directory you want to have this result and add the line:
AddType application/octet-stream .pdf
This will cause any file with the extention .pdf to download by default. You can even have some .pdf files on the page load in the browser while others download by using the FilesMatch directive ( http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess.html ).
I realize your original question said "how do I do it with PHP" but I thought I'd post in case you were looking for a simpler, more elegant solution. Do keep in mind any directives you put in an .htaccess file will also affect any sub-directories below it.

Categories