views:

66

answers:

1

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.

+1  A: 

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();
}
Tim
Thank you. I am so taken away that I didn't think of making a `readfile` call.
Nirmal