Downloaded file has bad mime-type - php

I have a PHP script that creates a Zip Archive and then passes it to the browser for download.
This can be easily done via HTTP Response Headers
header('Pragma: no-cache');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0, public')
header('Content-Type: application/zip')
header('Content-Length: ' . filesize($zipPath))
header('Content-Disposition: attachment; filename=filename.zip')
header('Content-Transfer-Encoding: binary')
echo file_get_contents($zipPath);
The file is correctly downloaded. I can also open it. However, when I check the downloaded file mime-type, it tells me : application/octet-stream instead of application/zip.
For file mime-type detection, I use the following snippet :
$pathToDownloadedFile = 'somewhere-on-disk';
$file = finfo_open(FILEINFO_MIME_TYPE);
var_dump(
file_exists($pathToDownloadedFile),
finfo_file($file, $pathToDownloadedFile)
);
which dumps out :
true
application/octet-stream
I googled the problem but with no luck.
Any piece of help would be very appreciated.

Related

corrupted file by saving with CodeIgniter and header output

I want to download a file from the upload folder by calling mydomein/download/filenem
It works in this way, that I received the wanted image, but I think I have a bug in the header which I output into my controller with this code:
$file=my_path_tofile;
header('Expires: 0');
header('Content-Description: File Transfer');
header("Content-type: ".$finfo->file($file));
//header("Content-type: application/".$ext);
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="'.$data['realname'].'"');
header('Cache-Control: must-revalidate');
header("Content-Length: " . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($file);
exit;
If I call the URL, the download will start, the filename is fine and the size looks right too. But if I want to open the file I get the message that the file could not open in cases of corruption.
I'm not an expert about headers, so can someone explain me what is the problem is and how I can solve it?

Redirect to a file outside of the domain Root

I want to give a file to a person based on the users rank so I need to hide the files in a directory which is hidden.
I'm using Plesk and my structure looks like this:
api (reachable from https://api.pexlab.net)
cloud (reachable from https://cloud.pexlab.net)
default (reachable from https://pexlab.net)
error_docs
hidden (not reachable)
My PHP script is located in:
api/hub/Test.php (reachable from https://api.pexlab.net/hub/Test.php)
I have tried this:
# In Test.php
downloadFile("../../hidden/hub/download/assets/user/main.fxml");
# Function:
function downloadFile($file) {
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;
}
}
This method works but I want to redirect to this file (show it) and NOT download it. So I have tried using this:
header("Location: ../../hidden/hub/download/assets/user/main.fxml");
But this tried to redirect to https://api.pexlab.net/hidden/hub/download/assets/user/main.fxml which is invalid.
The only difference between "viewing" and "downloading" a file is what the browser does with the data. Ultimately, that's in the hands of the user, but the server can indicate what it would like to happen.
I suspect you have copied these lines without really understanding what they do:
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));
These are all instructions to the browser telling it what to do with the data you send.
The Content-Disposition header is used to tell the browser "rather than trying to display this content straight away, suggest the user saves it in a file, with this name". To use the browser's default behaviour, you would simply leave off this header, or give it the value inline.
The Content-Type header tells the browser what type of file this is. The value application/octet-stream means "just a bunch of bytes, don't try to interpret them in any way". Obviously, that would be no good for viewing a file in the browser, so you should send an appropriate "MIME type", like text/html or image/jpeg, as appropriate for the file you're serving. I'm guessing "FXML" is an XML-based format, so text/xml might be appropriate; or if it's human readable and you just want it displayed without any formatting, use text/plain.

PHP Create Download Link

<?php
$file = SEVENCE_DATA_ROOT."/resource/devdemo/promo_card.tiff";
header('Content-Description: File Transfer');
header('Content-type: application/tiff');
header("Content-Type: application/force-download");// some browsers need this
header("Content-Disposition: attachment; filename=promo_card.tiff");
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;
?>
My issue is that the download image can't be viewed. Can anyone help me with this issue?
Content-Type: application/force-download");// some browsers need this
No browser does need this. It's a dirty glitch..
Reasons why your file cannot be opened:
You have a space or invisible characters at the beginning of your script
the path you are loading the file from might be wrong and it doesn't even read from it (what size is the downloaded file?)
the file simply is corrupt
you don't have a tiff-viewer
The correct mime type is image/tiff (instead of application/tiff)

PHP force downloading

How do I make php force download a file. I have a file named song1, which is a song, in the file songs. so from the page I am at it is song/song1. How do I make php download the file as soon as the php is ran?
You have to send out some HTTP headers:
header('Content-disposition:attachment; filename=song.mp3;');
Then you have to pull the song data with for example file_get_contents(). And finally use a die() or exit() to avoid adding extra data.
Side note: The above code will not work if you've already sent out HTTP headers (wrote out some whitespace characters, etc), so put it directly after <?php if you can.
Try below code
$file='song1.mp3';
if (file_exists('song/'.$file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('song/'.$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('song/'.$file));
ob_clean();
flush();
readfile('song/'.$file);
}
It will directly download file.

PDF rendered as text

I'm using PHP to generate a PDF via browser for my web-application. Recently, the client changed the webserver to Apache and now this feature is no longer working. Instead of generating the PDF, the browser is showing the PDF as text, just as it was ignoring Content-Type (that is set to "application/pdf"). In fact, I successfully simulated the problem by commenting the line setting the Content-Type in the source code.
I need ideas about where and what to look for, any help will be very welcome :)
Since you generate PDF files through PHP, you can try to add these headers:
$file_default = 'default filename you want to appear when the user downloads.pdf';
$file_location = '/path/to/file.pdf';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_default);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file_location));
ob_clean();
flush();
readfile($file_location);
I guess you'd have to force apache to download PDF content rather than showing:
check this: http://www.thingy-ma-jig.co.uk/blog/06-08-2007/force-a-pdf-to-download

Categories