Welcome Guest, Not a member yet? Register   Sign In
Problem with image download display
#1

[eluser]Mitchell[/eluser]
I'm trying to download and display an image stored in a directory above the web root--something I've done many times pre-CI (I've been experimenting with CI for a couple of weeks now). If I use an old pre-CI file located in the public_html directory, the image displays just fine.

View Code
Code:
<img src="/download_file.php">

download_file.php code
Code:
$file_name = 'pix.jpg';
$full_path = '/home/path/to/folder/file_name';
$file_size = filesize($full_path);
$file_type = 'image/jpeg';

if (file_exists($full_path))
{
  $fp = fopen($full_path, 'rb');
  header ("Content-Type: $file_type \n");
  header ("Content-Length: $file_size \n");

  fpassthru($fp);
  exit();
}

But if I use the download helper force_download(), the image doesn't display, but rather the binary file is displayed.

View code:
Code:
$this->load->helper('download');
$name = 'photo.jpg';
$data = file_get_contents('/home/path/to/folder/pix.jpg);
force_download($name, $data);

After a few hours of experimenting with anything I could think of, I took the old code that was correctly displaying the image and simply moved it from the public_html folder to the CI application folder which is above the web root and changed the view code appropriately. Interestingly, once I moved the working code to the application folder, it no longer worked and also displayed the binary output instead of showing the image.

There is probably something really stupid that I'm doing, but I'll be darned if I can figure out what it is.

Oh, the directory stucture is as follows with system and application above the web root.

system
application
public_html

Mitchell
#2

[eluser]alectrash[/eluser]
Hi I have same problem, I can force downloads of pdfs, mp3s, videos, but JPG IMAGES DONT WORK, THEY ARE ALWAYS CORRUPT and won't open. i have no idea why? it works fine outside of codeigniter, so it must be something to do with CI.

Here is normal image download code, which can work in a codeigniter function like so.

Code:
header('Content-type: image/jpeg');
   header('Content-Disposition: attachment; filename="'. $data['filename'] . '"');
   header("Content-Length: " . $data['filesize']);
   readfile("/var/www/files/" . $data['filename']);

but after thisdid not work I then tried:

Code:
$imagedata = file_get_contents("/var/www/files/" . $data['filename']);
$name = $data['filename'];
force_download($name, $imagedata);

but still it just wont work, bored of trying now...anybody have any ideas why this is not working?
#3

[eluser]spagi[/eluser]
Hi guys, I have same probleme as you. Can anyone advise me?




Theme © iAndrew 2016 - Forum software by © MyBB