Welcome Guest, Not a member yet? Register   Sign In
Problem forcing image downloads
#1

[eluser]universal_serial_bus[/eluser]
Hello there,

I'm having some problems when I force an image download...
Since my PHP application's default charset is UTF-8 (it's wrote in Spanish), when I try to force an image download, it's charset becomes UTF-8.

I realized that images just can't be viewed if their container (file) don't use ANSI codification.

The issue is that, for some unknown reason for me, my application generates UTF-8 encoded files when I force downloads... So, if I force an image download, it just can't be viewed until I change manually the file encoding from UTF-8 to ANSI.

I'm doing it with CI's force_download() function from Download helper, but in fact I've tried directly with PHP headers.
In addition, I'd to say that I tried to specify ISO-8859-1 in header("Content-type: MIME; Charset: ISO-8859-1"); and it didn't work.
I've tryied to load a ISO-8859-1 encoded view file and trigger force_download() from this despite of directly from controller, but it didn't work neither.
Always download a UTF-8 file.

I've checked other plain text files in ANSI and they're downloaded in UTF-8 as well.

So... I'm begin to being a lil' desperate...
Some idea of what is going on?

Thank you very much in advance !
#2

[eluser]pickupman[/eluser]
What happens when you try:
Code:
//set $image to your filename
header('Content-Type: image/jpeg');
header('Content-Length: 1234');
header('Content-Disposition: attachment;filename="'.$image.'"');
echo file_get_contents($image);
#3

[eluser]universal_serial_bus[/eluser]
[quote author="pickupman" date="1293490613"]What happens when you try:
Code:
//set $image to your filename
header('Content-Type: image/jpeg');
header('Content-Length: 1234');
header('Content-Disposition: attachment;filename="'.$image.'"');
echo file_get_contents($image);
[/quote]

Exactly the same fact: it generates an output file containing the image alphanumeric code, but this file (the container) is encoded into UTF-8...
I've tryied as last hope use some hacky-wise tricks like apply utf8_decode() to image code (the result of file_get_contents()Wink before outputting, but it doesn't work: the file remains in UTF-8.

Thanks for the try...
#4

[eluser]pickupman[/eluser]
Would you mind posting some of your code to make sure it may not be another issue for generating your images?
#5

[eluser]universal_serial_bus[/eluser]
[quote author="pickupman" date="1293493214"]Would you mind posting some of your code to make sure it may not be another issue for generating your images?[/quote]

Sure. That's my code:

Code:
if($token == md5($file->filename)) {
        $this->load->helper('download');
        $data = file_get_contents(base_url().DOCSPATH.$file->path.'/'.$file->filename);
        force_download($file->filename, $data);
} else {
        echo 'Invalid parameters.';
}

The first comparation is just for security reasons...
That's all... force_download() is a function from CI's download helper.
#6

[eluser]pickupman[/eluser]
You want to try and replace base_url() with FCPATH as it the constant the points to the CI index.php file. Using base_url() maybe messing up file_get_contents() as it may be restricted by your php.ini configuration. It doesn't not have a trailing path, so you will need to make sure that DOCSPATH begins with a "/".

If you echo the string inside of file_get_contents(), is the path correct. I checked my application/config/config.php and charset is set to UTF-8 as well. I used the force_download() helper, and it worked fine on a jpg file.
#7

[eluser]universal_serial_bus[/eluser]
[quote author="pickupman" date="1293500629"]You want to try and replace base_url() with FCPATH as it the constant the points to the CI index.php file. Using base_url() maybe messing up file_get_contents() as it may be restricted by your php.ini configuration. It doesn't not have a trailing path, so you will need to make sure that DOCSPATH begins with a "/".

If you echo the string inside of file_get_contents(), is the path correct. I checked my application/config/config.php and charset is set to UTF-8 as well. I used the force_download() helper, and it worked fine on a jpg file.[/quote]

First of all, thanks for the troubles.

The routing code to the file is ok. The application download the files correctly.
The problem is that it converts EVERYTHING in UTF-8... and that's annoying.

If you have checked this portion of code into your own application and you had no problems, I guess I'm suposed to think the problems is in my CI or server configuration...

I'll be working on it. Anyway, do you have any idea what can it be, guys? girls?
#8

[eluser]pickupman[/eluser]
Where do you have your character encoding set at? If it is in your php.ini,you could try using ini_set() to change it. Otherwise, as noted I have the same setting in my config.php file.
#9

[eluser]universal_serial_bus[/eluser]
[quote author="pickupman" date="1293502061"]Where do you have your character encoding set at? If it is in your php.ini,you could try using ini_set() to change it. Otherwise, as noted I have the same setting in my config.php file.[/quote]

Ok, that's weird.
I've changed all my explicit defined encoding charset, and put everyone to ISO-8859-1. That's:
- PHP.ini on default_charset.
- In addition, I wrote ini_set('default_charset', 'ISO-8859-1'); on my rutine.
- Also added the header header ('Content-type: text/html; charset=ISO-8859-1'); on my rutine.
- Changed the config.php file parameter to $config['charset'] = "ISO-8859-1";

...and the script keeps outputting the images in UTF-8...

I've checked that in my Apache httpd.conf isn't specified any default charset. Also isn't it on my .htaccess file...

Anyway, it holds on. Now I'm officially desperate.

After made changes I've erased cookies, prefs and so from my browser, and restarted Apache service, and changes had been applied because of the codification of my views... but the output files keep acting themselves...

Please, pickupman, can you have this try? Can you force download from both an original UTF-8 and ANSI encoded file? Can you check which is its two encoding charsets after downloading...?

Thank you very much, dude. I'm freaking out.
#10

[eluser]pickupman[/eluser]
My php.ini had an empty default_charset, and I have edited it to be UTF-8. Check phpinfo() and it is displaying UTF-8 for character encoding. I am using this basic code to test with:
Code:
$this->load->helper('download');
$data = file_get_contents(FCPATH . '/user_guide/images/ci_logo_flame.jpg');
force_download('ci_logo_flame.jpg', $data);

I am not sure about your encoding issue about UTF-8 and ANSI. I do know regardless, that the above code produces a same image dialog in the browser no matter what I use in my php.ini. I am able to view the image without issue. Try a simple example like this and work backwards to see what your issue may be. The example uses the CI logo located in the user guide folder.




Theme © iAndrew 2016 - Forum software by © MyBB