How to force image loading in a browser?

I want to force the user to upload images. It does not open in the browser.

You can use HTML5 for this download attribute, but only Chrome currently supports it.

I tried the .htaccess solution, but it does not work.

 <Files *.jpg> ForceType application/octet-stream Header set Content-Disposition attachment </Files> 

How can I force download all my images if the user clicks on the link?

 <a href="http://blablabla.com/azerty/abc.jpg" target="_blank" />Download</a> 
+7
source share
2 answers

There are two ways to do this: one with JS, one with PHP.

In JS from this site :

 <a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'file.html');" >Save this page</a> 

In PHP, create a script called download.php that looks like the following code:

 <?php // Force download of image file specified in URL query string and which // is in the same directory as the download.PHP . if(empty($_GET['img'])) { header("HTTP/1.0 404 Not Found"); return; } $basename = basename($_GET['img']); $filename = __DIR__ . '/' . $basename; // don't accept other directories $mime = ($mime = getimagesize($filename)) ? $mime['mime'] : $mime; $size = filesize($filename); $fp = fopen($filename, "rb"); if (!($mime && $size && $fp)) { // Error. return; } header("Content-type: " . $mime); header("Content-Length: " . $size); // NOTE: Possible header injection via $basename header("Content-Disposition: attachment; filename=" . $basename); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); fpassthru($fp); 

Then set the image link to point to this file as follows:

 <img src="/images/download.php?img=imagename.jpg" alt="test"> 
+11
source

Try this .htaccess:

 <FilesMatch "\.(?i:jpg|gif|png)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch> 
-2
source

All Articles