readfile doesn''t work for URL path - php

I used readfile to read the file but its not working for URL path. So, I gave the physical path for it. How to use URL path ?
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($path) . "\"");
readfile($path);

If php.ini has enabled the option called
allow_url_fopen=On
Then url base path will work in readfile other wise you will have to use fopen.
Thanks
Amit

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");

Forcing file download in PHP corrupts zip files

My PHP script forces a file to download, which all works fine, but when forcing zip files to download, they corrupt.
When I try and open the zip file locally it just converts from a .zip to a .zip.cpgz
If I download the file from a direct URL the zip file is fine, so I know it's only corrupting during the push/download phase.
Here's my code
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"_zip_download.zip\"");
header("Content-Transfer-Encoding: Binary");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($zip_file));
For zip file you can set header("Content-Type: application/zip");
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"_zip_download.zip\"");
header("Content-Length: " . filesize($zip_file));
readfile($zip_file);
exit;

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.

Categories