Force browser to download - php

I'm using a .htaccess file to make links to JPEGs download instead of opening in the browser window.
This is what I'm using:
AddType application/octet-stream .jpg
It's working fine in Safari and Firefox on Mac OSX but not on IE 8 on Windows.
Is there any way around this apart from writing a header in PHP which I would rather not do?

I think Internet Explorer does also expect Content-Disposition: attachment for downloads. If you have mod_headers available, you can do this:
<FilesMatch "\.jpg$">
Header set Content-Disposition attachment
Header set Content-Type application/octet-stream
</FilesMatch>

I found some tutorials which use another method that should work for you:
Here
and Here

Add header("Content-disposition: attachment; filename=..."); in PHP

Related

How to set Content-Length header with php.ini / httpd.conf / .htaccess?

I've some pdf files on my shared hosting server of bluehost. I need to download them from iOS app & I want to show the download progress bar.
But I couldn't because Content-length is not present in Header response.
How can I get this?
You can set it via header. With the header function you can set HTTP information. But note that you dont have any output before set HTTP informationen.
header('Content-type: application/pdf');
header("Content-length: $size");
header('Content-Disposition: attachment; filename="downloaded.pdf"');
Edit:
If you dont use PHP to download the file you can use SetEnv no-gzip dont-vary in your htaccess to suppress GZIP.

htaccess opening php files as downloads

i have had a search around but haven't been able to find a similar problem.
When i add the following to my htaccess file (i want to restrict upload types to my server)
ForceType application/octet-stream
Header set Content-Disposition attachment
<FilesMatch "(?i)\.(doc?x|txt|xls?x|csv|pdf|ppt?x|zip|gif|jpe?g|png)$">
ForceType none
Header unset Content-Disposition
</FilesMatch>
Header set X-Content-Type-Options nosniff
I then refresh my page it then opens the download dialog box instead of showing the php page. I though it might be the deflate mod compressing the htaccess onto 1 line but im not so sure now.
could anyone point me in the right direction?
Thanks
Thanks to the comments above i have sorted the problem. it was my mistake

FilesMatch downloading files instead of loading

Rightio...I am about to add an image upload to my site. After reading some security blogs I have added the following to my htaccess file.
ForceType application/octet-stream
<FilesMatch "(?i)\.jpe?g$">
ForceType image/jpeg
</FilesMatch>
<FilesMatch "(?i)\.gif$">
ForceType image/gif
</FilesMatch>
<FilesMatch "(?i)\.png$">
ForceType image/png
</FilesMatch>
When I browse to a PHP page it downloads the file.php instead of showing the page in the browser.
Any ideas would be much appreciated.
I am working on MAMP locally.
Many thanks
Lewis
When I browse to a PHP page it downloads the file.php instead of showing the page in the browser
That's because you have this line:
ForceType application/octet-stream
by itself, without any condition. That means, everything should have the mime-type application/octet-stream, which browsers see and understand as binary content that should be downloaded. Not sure how this is a security improvement as it makes it so your entire site can be downloaded and viewed as code, as opposed to any dynamically generated content.
You have to put that .htaccess code in a directory that only has image files. If you don't have your images in their own directory, create a directory for them and put them there (and update any links to them).

This page is downloaded when the site is visited

Try to open it with Safari or IE
webtelevideo.com
Chrome has no problems
What does it depend?
The server does not identify the page as HTML:
Content-Type: text-html;charset=UTF-8
The appropriate MIME type is text/html, not text-html. It's a serious server misconfiguration.
as Álvaro G. Vicario stated, the w3c validator indicates between other things:
The document is being served with the text-html Mime Type which is not a registered media type for the Document Type. The recommended media type for this document is: application/xhtml+xml
i suggest you to valide your site structure through http://validator.w3.org/
I think it's because of the apache configuration try adding
AddType application/x-httpd-php .php
Got absolutely no idea what exactly you're position is with this... Are you the developer? I assume not, however, unable to load in IE, until I changed the URL to: http://www.webtelevideo.com/Programmi-TV/Martin - May work with other things, but up to you to check.

Why is gzip compression with Internet Explorer not working?

My site is gzipped compressed, and it doesn't load properly in IE. It works fine in FF/Chrome, but in IE, the only thing that pops up is a box asking to download the .gz file which contains the html document for the page.
Is this normal? Do I have to turn off gzip?
Are you sending the correct headers?
You need to send the
Content-Encoding: gzip
header for IE to understand that it is gzipped (Firefox, et al are smart enough to detect this automatically - even though they shouldn't!)
In PHP, you can do this using:-
header('Content-Encoding: gzip');
One thing to add - you should turn off gzip compression for IE6 pre-SP2. Before SP2, IE6 doesn't always read and cache gzipped content properly and you end up with mangled code.
You can identify an IE6 SP2 install by looking for "SV1" in the user-agent string.
I have seen problems when using gzip with Internet Explorer on a page that has flash on it. If your page has flash this may be why. I don't remember the cause and at the time we found it it was causing problems on a live site so we just disabled gzip for Internet Explorer to get around it.
The HTTP headers are the issue. If you have the gzip header along with one of the following:
Vary
Transfer Encoding: Chunked
one or both need to be removed.
This problem is more likely to occur on a computer that is running Apache HTTP Server because Apache HTTP Server can use chunked encoding on any kind of file. This includes static files such as a JavaScript file or a .gif file. When the problem that is described in this article occurs, the content that is stored in the Internet Explorer cache may be truncated or corrupted.
For XML, XHTML, and XSLT files, prevent parsing as text/html or text/xsl:
RewriteCond %{HTTP_ACCEPT} text\/html [OR]
RewriteCond %{HTTP_ACCEPT} text\/xsl [OR]
RewriteCond %{HTTP_ACCEPT} gif|jpeg|png$
ReWriteRule .*\.(xsl|xslt)$ - [F]
And add application/xml as a content type mapping:
AddType application/xml .xsl
References
Internet Explorer may not decompress HTTP content when you visit a Web site
Vary with Care
IE 8 seems unable to display some Wikipedia Pages - application/x-gzip-compressed MIME type
Internet Explorer 6 does not display XHTML strict Web pages correctly

Categories