CodeIgniter Forums
Is there any way to download a image when it is clicked? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Is there any way to download a image when it is clicked? (/thread-37028.html)



Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]chefnelone[/eluser]
hello

Just that. Any class, helper, plugin to to download a image when it is clicked?


I used to use this: (but since CI doesn't allow GET it doesn't work)
Code:
<?php
// Force download of image file specified in URL query string and which
// is in the same directory as this script:
if(!empty($_GET['img']))
{
   $filename = basename($_GET['img']); // don't accept other directories
   $size = @getimagesize($filename);
   $fp = @fopen($filename, "rb");
   if ($size && $fp)
   {
      header("Content-type: {$size['mime']}");
      header("Content-Length: " . filesize($filename));
      header("Content-Disposition: attachment; filename=$filename");
      header('Content-Transfer-Encoding: binary');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      fpassthru($fp);
      exit;
   }
}
header("HTTP/1.0 404 Not Found");
?>


Thanks

and... Merry Christmas to everyone!


Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]Twisted1919[/eluser]
you can pass the filename as uri segment. That's what i do.
Beside, i have .html extension, so the download url becomes :
/download/image/large/md5-of-the-file-name.EXT.html


Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]chefnelone[/eluser]
[quote author="Twisted1919" date="1293212156"]you can pass the filename as uri segment. That's what i do.
Beside, i have .html extension, so the download url becomes :
/download/image/large/md5-of-the-file-name.EXT.html[/quote]

I'll try what you say.


Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]cereal[/eluser]
Or you can create a controller "download" like this:

Code:
<?php

class Download extends Controller {

    function Download()
    {
        parent::Controller();
        $this->load->helper(array('url', 'download'));
    }
    
    function image()
    {
        @ob_end_clean();
    $data = file_get_contents($this->input->server('DOCUMENT_ROOT') . 'images/' . $this->uri->segment(3));
        $name = $this->uri->segment(3);
        force_download($name, $data);
    }
}
?>

To download just call: http://your.server/download/image/name_of_file.jpg

This way you can add a session check to prevent direct linking, keep track of downloads, you can hide the real path and you can use an .htaccess file to prevent downloads from the images/ directory. Bye Smile


Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]chefnelone[/eluser]
[quote author="cereal" date="1293213988"]Or you can create a controller "download" like this:

Code:
<?php

class Download extends Controller {

    function Download()
    {
        parent::Controller();
        $this->load->helper(array('url', 'download'));
    }
    
    function image()
    {
        @ob_end_clean();
    $data = file_get_contents($this->input->server('DOCUMENT_ROOT') . 'images/' . $this->uri->segment(3));
        $name = $this->uri->segment(3);
        force_download($name, $data);
    }
}
?>

To download just call: http://your.server/download/image/name_of_file.jpg

This way you can add a session check to prevent direct linking, keep track of downloads, you can hide the real path and you can use an .htaccess file to prevent downloads from the images/ directory. Bye Smile[/quote]

I tried but I'm getting this error:

ErrorException [ Warning ]: file_get_contents(http://192.168.1/ci/uploads/galeriasFotos/image_jpg) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
The image is in /uploads/galeriasFotos/

I can see that the problem is that the controller change image.jpg to image_jpg.
Why is that?
html:
Code:
<a href="http://192.168.1/ci/download/image/image.jpg"></a>



Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]cereal[/eluser]
192.168.1 is a wrong IP, try with the correct one, you are getting a 404 error, let me know, bye Smile


Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]chefnelone[/eluser]
[quote author="cereal" date="1293216197"]192.168.1 is a wrong IP, try with the correct one, you are getting a 404 error, let me know, bye Smile[/quote]

you right... but I just wrote the wrong one, correct one is : 192.168.1.33
but still getting same error message

What about the change from '.' to '_'
I think this is the point but don't know how to fix it...

also keep in mind that the image is in /uploads/galeriasFotos/


Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]cereal[/eluser]
I don't know why it's changing from dot to underscore but I believe something should be change in your Apache config, In my experience CI never worked good in a local server running multiple sites without configuration. If you want to try, add a site following the instructions in point A, otherwise jump to the test at the end of my post (point B).

A) You should create a file in sites-available/ where you set the Document Root and the Server Name:

Code:
<VirtualHost *:80>

  # Admin email, Server Name (domain name) and any aliases
  ServerAdmin [email protected]
  ServerName  test.local
  #ServerAlias www.test.local


  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.php
  DocumentRoot /var/www/your_local/httpdocs/


  # Custom log file locations
  LogLevel warn
  CustomLog /var/www/your_local/logs/access.log combined
  ErrorLog /var/www/your_local/logs/error.log

</VirtualHost>

Change the hosts file, so your browser will point to the local CI installation:

Code:
127.0.0.1 test.local

And use a2ensite to set the new config, after that you should restart your server:

Code:
/etc/init.d/apache2 restart

After that you should be able to call uri like this:

Code:
http://test.local/download/image/image.jpg

This makes the local CI installation available from the other workstations on the same lan, just update the hosts on each workstation with the ip of the server:

Code:
192.168.1.33 test.local

B) Do a test, change:

Code:
$data = file_get_contents($this->input->server('DOCUMENT_ROOT') . 'uploads/galeriasFotos/' . $this->uri->segment(3));
$name = $this->uri->segment(3);

to:

Code:
$data = file_get_contents('http://192.168.1.33/ci/download/image/image.jpg');
$name = 'image.jpg';

If goes fine than, in the original, try to change uri->segment() to 4, or give a check to your base_url in config.php
With file_get_contents you can use the DOCUMENT_ROOT or the uri, but the write one should be http://192.168.1.33/download/image/image.jpg

Code:
base_url() . 'download/image/' . $this->segment->uri(3)

Other than this I don't know how to help you. Bye.


Is there any way to download a image when it is clicked? - El Forum - 12-24-2010

[eluser]chefnelone[/eluser]
I just used srt_replace to replace _jpg to .jpg, now is working right,
Thanks