Link, which always offers to save/open....

J

jodleren

Hi all

I have seen this, and now I need it myself.

I need a link to some files, which should not be opened by the browser
or plugins. I want a window to pop up and the user should choose the
action.
Several email services have this. I have been looking into, but not
figured it out.

Any ideas?

BR
Sonnich
 
J

jodleren

Google: HTTP Content-Disposition header

You could be a little more specific...

I found something and came up with:

<?php
$file=$_GET['file'];
header('Content-type: application/force-download');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
?>

Though, this opens a window (when I use this from another link), is
there a way to avoid the browser window?

BR
Sonnich

Please CC to sonnich_at_hot_._ee
 
T

Toby A Inkster

jodleren said:
You could be a little more specific...
I found something

Firstly, I was clearly sufficiently specific, in that it lead you to an
answer.

Secondly, I couldn't be very much more specific than I was, as you didn't
mention any details of your hosting situation in your original post.
(Operating system, server software, scripting languages, level of access,
etc.)
and came up with: [...]
header('Content-type: application/force-download');

Replace that line with a real content type. For example, if the file is a
PDF, then the Content-Type should be "application/pdf". If it's a JPEG,
then "image/jpeg". If you are really unable to determine the correct
content type, use "application/octet-stream".

Using made-up content types like the one you've used above may seem to be
"convenient", but it harms the web as a whole.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
 
J

jodleren

and came up with:
[...]
header('Content-type: application/force-download');

Replace that line with a real content type. For example, if the file is a
PDF, then the Content-Type should be "application/pdf". If it's a JPEG,
then "image/jpeg". If you are really unable to determine the correct
content type, use "application/octet-stream".

Using made-up content types like the one you've used above may seem to be
"convenient", but it harms the web as a whole.

Since I can have _any_ file, where is there an overview of those or
trick to get them?

And since I open this from another window, is the a way to have a
link, which goes directly to the open/save dialog. That is what I
really need.

BR
Sonnich
 
J

Jonathan N. Little

jodleren said:
and came up with: [...]
header('Content-type: application/force-download');
Replace that line with a real content type. For example, if the file is a
PDF, then the Content-Type should be "application/pdf". If it's a JPEG,
then "image/jpeg". If you are really unable to determine the correct
content type, use "application/octet-stream".

Using made-up content types like the one you've used above may seem to be
"convenient", but it harms the web as a whole.

Since I can have _any_ file, where is there an overview of those or
trick to get them?

And since I open this from another window, is the a way to have a
link, which goes directly to the open/save dialog. That is what I
really need.

This question gets ask quite often, a a specific media is handled by the
browser (open in the browser, open directly by the app or download) is
browser setting so if the user has set it to open in the browser there
is not much you can do about it.
 
J

jodleren

You could be a little more specific...
I found something

Firstly, I was clearly sufficiently specific, in that it lead you to an
answer.

Secondly, I couldn't be very much more specific than I was, as you didn't
mention any details of your hosting situation in your original post.
(Operating system, server software, scripting languages, level of access,
etc.)


and came up with: [...]
header('Content-type: application/force-download');

Replace that line with a real content type. For example, if the file is a
PDF, then the Content-Type should be "application/pdf". If it's a JPEG,
then "image/jpeg". If you are really unable to determine the correct
content type, use "application/octet-stream".

Would there be a problem with getting the extension of the file and
using it, such as:
$ext = get_extension_of_file;

header("Content-type: application/$ext");
Using made-up content types like the one you've used above may seem to be
"convenient", but it harms the web as a whole.

What do you think about this?
header("Content-Disposition: attachment; filename=\"$file\"");

Is there a place where headers are listed?
I could use that now

BR
Sonnich
 
J

jodleren

jodleren wrote:

This question gets ask quite often, a a specific media is handled by the
browser (open in the browser, open directly by the app or download) is
browser setting so if the user has set it to open in the browser there
is not much you can do about it.

Well... I am playing around with this again...

my mail, www.hot.ee, has a way of doing it... but I have a feeling
that they do it in some other way.
Also, I recall lately, a window, which opens, then is closes/dialog
opens, so only the dialog remains...
There is a way out there...

BR
Sonnich
 
J

John Hosking

jodleren said:
Would there be a problem with getting the extension of the file and
using it, such as:
$ext = get_extension_of_file;

header("Content-type: application/$ext");

The MIME type doesn't always (or even usually) match the file extension.
What do you think about this?
header("Content-Disposition: attachment; filename=\"$file\"");

You'd also need to set the content-type to something nasty[1]. Please
don't.
Is there a place where headers are listed?

Google is your friend. Well, it's *our* friend, but you could use it too.

Take a look at http://www.w3schools.com/media/media_mimeref.asp. I
cannot vouch for the veracity of the content in that table, since
www.w3schools.com isn't always accurate, but it will give you an idea of
what you're dealing with. Note especially the links at the top of the
page, leading you to the RFC (Request For Comment) documents which are
the source of this information.

[1]http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index3.html
 
T

Travis Newbury

I have seen this, and now I need it myself.
I need a link to some files, which should not be opened by the browser
or plugins. I want a window to pop up and the user should choose the
action.

This is my choice. You don' t have any say in what I do with the file.
 
A

Andy Dingley

I need a link to some files, which should not be opened by the browser
or plugins.

Browsers only do this if two things happen: They know what type the
document claims to be, and they know a way to handle this type.

If they don't have both of these, then they are forced to ask the user
what to open it with / save it as a file (or throw it away!).

So if you serve a document (any document, even HTML) with the HTTP
content-type of application/octet-stream, all the browser can do is
handle it as raw bytes. It will almost certainly allow it to be saved,
it might give an option to select a program to open it with.

If you have a scripting language on the server, then you can do this
easily. If you don't, but you're using Apache, then you can
configure .htaccess to serve this content-type for all files with
either a particular file extension, or files from a particular
directory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top