Welcome Guest, Not a member yet? Register   Sign In
codigniter and image download problem
#1

[eluser]alectrash[/eluser]
Hi there, was wondering if anybody knows how to download image with codeigniter.

I can force downloads of pdfs, mp3s, videos, but JPG IMAGES DONT WORK, THEY ARE ALWAYS CORRUPT and won’t open. I

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 this did not work for images I then tried:

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

again it works for pdfs, mp3s, archives etc, but just not jpeg image files?

Does anybody have any ideas? much appreciatd at a dead end with this know.
#2

[eluser]alectrash[/eluser]
is anybody else having image download problems with corrupt jpegs at all, I can download anything but images....I am really baffled by this one guys.
#3

[eluser]Max Binshtok[/eluser]
This is probably because there is a blank line added at the start of the downloaded images. I am having this problem with DOC files, however even though I know now WHAT the problem is, I can't fix it... Sad
#4

[eluser]simonmaddox[/eluser]
[quote author="alectrash" date="1225476483"]Hi there, was wondering if anybody knows how to download image with codeigniter.

I can force downloads of pdfs, mp3s, videos, but JPG IMAGES DONT WORK, THEY ARE ALWAYS CORRUPT and won’t open. I

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 this did not work for images I then tried:

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

again it works for pdfs, mp3s, archives etc, but just not jpeg image files?

Does anybody have any ideas? much appreciatd at a dead end with this know.[/quote]

Try commenting out the Content-Type header - you might find that unexpected things are being output - look out for newlines.
#5

[eluser]dmorin[/eluser]
Also, in the download helper, try commenting out anything that sets the content-length. If you have mb string functions overloaded, the content length can be calculated incorrectly and will cut off the end of the file download. Probably not the issue in your case but is worth testing.

Also, how are you fetching/generating the images you are trying to output?
#6

[eluser]Unknown[/eluser]
I've found that if you call the force_download function from a controller rather than a view it seems to work fine. When handing off to the view, there must be a function that pre-pends a couple of characters making your images corrupt. I'm using the other force_download helper that has been floating around and this code seems to work fine in jpgs.

Code:
$url = base64_url_decode($url);
    $path = dirname(FCPATH) . '/' . $url;
    $filename = explode("/",$url);
    $filename = end($filename);
    if(file_exists($path))
    {
        force_download($path);
    }
    else
    {
        show_404('this page');
    }

Hope that helps!




Theme © iAndrew 2016 - Forum software by © MyBB