开发者

Telling the browser to open or download a file in Codeigniter

In Codeigniter, we can use the force_download($file_name,$data) function from the download helper to force the browser to download a file.

But this is not what I need, I would like Codeigniter to respond a http开发者_如何转开发 header with the MIME type that the file corresponds to, so that the browser may or may not download the file, depending on whether there is an application registered for that MIME type on the client machine (if there is one, that application will open that file).

Is there a way of doing this with Codeigniter?

Many thanks to you all.


No, you cannot do that with Codeigniter.

Generally speaking, with server-side technologies like PHP, you can only send content to the client, not directly interact with the client. It is up to the client to do what they wish with whatever piece of content you send them. Even with client side tools like javascript, (thankfully) you can only have limited interaction with the client machine. What you are wanting to do is allow a website to (automatically) upon visiting, download a piece of software, and execute it on the client machine...

that's called malware... (brought to you by the makers of ActiveX)

Above was in direct response to

"...so that the browser may or may not download the file, depending on whether there is an application registered for that MIME type on the client machine (if there is one, that application will open that file).

UPDATE

You can set the mime type through plain php header manipulation. The following example is taken directly from the php header() documentation as the first example.

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜