Mime-type of downloading file - php

i'm trying to create downloadable video-files. In my site there is a list of files.
All videos are in .flv-format (flash). There is exact link to the file for the all videos.
But in all browsers after clicking content is loading to the browser`s window. I needn't in this. As i understand i should create redirect-page wich contains mime-type of the download file. What exactly should i do?
Language: php

The recommended MIME type for that is application/octet-stream:
The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data. […]
The recommended action for an implementation that receives an "application/octet-stream" entity is to simply offer to put the data in a file, with any Content-Transfer-Encoding undone, or perhaps to use it as input to a user-specified process.

Create a PHP page with the following:
<?php
$filepath = "path/to/file.ext";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filepath");
header("Content-Type: mime/type");
header("Content-Transfer-Encoding: binary");
// UPDATE: Add the below line to show file size during download.
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
?>
Set $filepath to the path of the file to be downloaded, and set Content-Type to the mime type of the file being downloaded.
Point the "download" link to this page.
For multiple files of the same type:
<?php
$filepath = $_GET['filepath'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filepath");
header("Content-Type: mime/type");
header("Content-Transfer-Encoding: binary");
// UPDATE: Add the below line to show file size during download.
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
?>
Replace the information as specified above, and point the "download" link to this page with a GET parameter named "filepath" containing the file path.
For example, if you name this php file "download.php", point the download link for a file named "movie.mov" (in the same directory as download.php) to "download.php?filepath=movie.mov".

Related

PHP - Force download or view in browser depending on file type

I am using the following headers to force a download but I need to try and have the browser display certain files like PDF's and JPG's if that is the file type, finding the exntension is easy enough but how can I alter these headers to open the file in the browser?
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header);
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");
In order to display a file in the browser, you'll need to use the correct MIME type. You can set it yourself based on file extension, or you can use the finfo module:
function getContentType($filename) {
$finfo = new finfo(FILEINFO_MIME);
return $finfo->file($filename);
}
header("Content-Type: " . getContentType($filename));
Without this, the browser will probably assume that it can't handle the application/octet-stream content, and force a download anyway.
You should also only send the Content-Disposition header if you want to force the file to be downloaded. If you remove that header, then the browser can decide if it should display the file or download it.

PHP send file only containing the extension

I have some problems with sending files with PHP. I would like to send a file named .version (Note the dot and nothing infort of it). The file is sent correctly but my browser renames it to version. Therefore the file looses its functionality. The file has to be named like this.
This is the code I use:
<?php
header("Content-Description: File Transfer");
header("Content-Type: text/version");
header("Content-Disposition: attachment; filename=\".version\"");
header("Content-Length: " . filesize($file));
readfile($file);
exit();
?>
$file is the correct internal path to the file.
Other headers like the expires header are set in my server config.
If there is information missing I will add if if you ask for it!

Get zip file from url (PHP)

I have a zip files that I want users to be able to download. The trick is I don't want the users to see what the url is and I don't want to download the file to my server.
So I want users to click a link like this:
http://example.com/download/4
which server-side accesses my S3 bucket with this url:
https://s3.amazonaws.com/my-bucket/uploads/4.zip
I've tried cURL, using S3 methods, and various headers() in my download($file_id) function but can't get this to work. This has to be easy, right?
Your right, its quite easy. Probably you will have to write something like this:
$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/x-compressed"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
You set various headers to make your user download the .zip. Afterwards you put your file into the output buffer with readfile() Afterwards you end your script with exit() for security's sake. This should work for you! Remember to change the path to your file.
Thanks #Xatenev for the help. This is actually what worked for me:
$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/zip"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Content-Type: " . $mm_type);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
readfile($path); // outputs the content of the file
exit();

export and save dialog box for images

I am dynamically generating image in php. The image has a fixed name. I wants a button or hyperlink and onclick of that button, users should be able to export image rather than right click and save as image options. The problem is that in case of excel,pdf or doc files, I can specify the path of file and browser automatically asks for the open or save option but for images, it opens them in separate window.I want same dialog box for saving the image as for the other files like excel,pdf.Please help me on this.
Thanks
you can set the header to force download (for PHP):
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: image/png");
Then you can read the file
readfile($file);
What you can do is force image download using .htaccess if you are using Apache or directly via php using header tags
PHP Example
$file = "PATH TO FILE" ;
$fileName = basename($file);
$fileSize = filesize($file);
set_time_limit(0);
header("Pragma: public");
header("Expires: 0"); // Cache Options
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // Cache Options
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\" " . $fileName ."\"");
header("Content-length: $fileSize");
readfile($file);

Illustrator opening as PDF

I am coding a file sharing application for my office. One strange problem I am going through is the Illustrator files being opened in PDF when you hit the download button.
This problem is triggered because the mime type of illustrator files is application/pdf. So the browser when it reads the file, triggers Acrobat to open the file. Is there any way I could instruct the browser to open the file in Illustrator?
Or is there any way to modify the mime type after uploading the file? The backend code is PHP.
Thank you for any help.
One way to do this is to force the browser to display the "download file"-dialog. So the user can decide what to do with the file.
This can be done via PHP-Headers. (http://www.php.net/manual/en/function.header.php#83384)
There is also an example on how to this (Post 83384):
<?php
// downloading a file
$filename = $_GET['path'];
// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");
/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
#readfile($filename);
exit(0);
?>
When using this example please consider that using
$filename = $_GET['path'];
is a big security problem. You should work with something like ID's instead or validate the input.
For example:
if($_GET['file'] == 1) {
$filename = foobar.pdf;
} elseif($_GET['file'] == 2) {
$filename = foo.pdf;
} else {
die();
}

Categories