PHP Downloads Some Empty Files and Some Normally - php

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

Related

How to download files from mysqli to web server directory?

I have a database on a web server, it has binary files stored in it, I need to download these files to the server directory, then change from bin to a PHP extension to capture data that appears in character blocks, I can do the whole process manually but I need to automate it, I don't know if it is possible to download files from the DB to the server's directory, or change its extension using PHP, I want to avoid discussions about the binary file, the focus is on these two questions if it is possible or not using PHP.
I know that there is this code to download from the server directory to the client, but I can't find a download from the DB to the server directory.
<?php
if(!empty($_GET['file'])){
$filename = basename($_GET['file']);
$filepath = 'downloads/' . $filename;
if(!empty($filename) && file_exists($filepath)){
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/zip");
header("Content-Transfer-Emcoding: binary");
readfile($filepath);
exit;
}
else {
echo "This File Does not exist.";
}
}

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;

PDF file not correctly encoded when downloading a pdf file from php

This might be a duplicate but I have searched for the solution for so long and I still could not solve my problem. I have a localhost server where it stored some pdf files for download.
I implemented the following function to force the download:
if(isset($_POST['dlPDF']))
{
$file = $_SERVER['DOCUMENT_ROOT'] .'/Upload/'.$pdfName;
header("Content-Type: application/pdf");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$pdfName");
header("Content-Transfer-Encoding: binary");
// read the file from disk
readfile($file);
}
However I could not open the pdf file properly since it says "the pdf file is not correctly encoded".
FYI, the downloaded pdf file is always a few Kb larger than the original file.
Please kindly let me know if there is anything I can do to make the pdf readable and I appreciate your help.

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.

How To Download File TO Desired FOLDER/Path?

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 :)

Categories