Welcome Guest, Not a member yet? Register   Sign In
set_header() file download problem
#1
Exclamation 
(This post was last modified: 12-29-2015, 02:43 AM by Rashid.)

CI 3.0.3. Here is my question on stackoverflow: http://stackoverflow.com/questions/34432...-size-n-kb

Further investigation revealed the fact that CI's set_header() method doesn't send 'Content-Disposition' header:

[Image: YEnEXOv.png]

In other words, $this->output->set_header() does not work, while php's native header() with the same param - works.

CAUSES PROBLEM:
PHP Code:
$this->output->set_header('Content-Description: File Transfer');
$this->output->set_header('Content-Type: application/octet-stream');
$this->output->set_header('Content-Disposition: attachment; filename="Segmentation ' $date->format('Y-m-d H:i:s') . '.' $format '"');
$this->output->set_header('Expires: 0');
$this->output->set_header('Cache-Control: must-revalidate');
$this->output->set_header('Pragma: public');
$this->output->set_header('Content-Length: ' strlen($str));
echo 
$str;
exit(); 

CORRECT:
PHP Code:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="Segmentation ' $date->format('Y-m-d H:i:s') . '.' $format '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' strlen($str));
echo 
$str;
exit(); 

Is it bug?
Reply
#2

(This post was last modified: 12-29-2015, 05:31 AM by orionstar.)

It isn't a bug, you call exit() after you define the headers, but CodeIgniter doesn't set the headers and not send back anything to the browser until everything in the controller's action is executed... You shouldn't use exit() and it will work!

+1 To set the content type, you can use the set_content_type() method.

++1 CodeIgniter has a download helper Wink http://www.codeigniter.com/user_guide/he...elper.html
Reply
#3

(This post was last modified: 12-29-2015, 06:52 AM by Rashid.)

Commenting exit() and letting function end normally does not solve the problem, i guess it IS a bug, yet..

Here is a full code:

PHP Code:
public function download($format$str)
{
    
$get_string rawurldecode($str);
    
parse_str($get_string$get_array);
    
    
$webinar_id $get_array['webinar_id'];
    unset(
$get_array['webinar_id']);
    
    
$date = new DateTime();
    
    
$str iconv('UTF-8''windows-1251'$this->segmentation_model->do_query($webinar_id$get_array$format));
    
$this->output->set_header('Content-Description: File Transfer');
    
$this->output->set_header('Content-Type: application/octet-stream');
    
$this->output->set_header('Content-Disposition: attachment; filename="Segmentation ' $date->format('Y-m-d H:i:s') . '.' $format '"');
    
$this->output->set_header('Expires: 0');
    
$this->output->set_header('Cache-Control: must-revalidate');
    
$this->output->set_header('Pragma: public');
    
$this->output->set_header('Content-Length: ' strlen($str));
    echo 
$str;


And what Chrome receives (Response Headers):

Code:
HTTP/1.1 200 OK
Date: Tue, 29 Dec 2015 13:25:37 GMT
Server: Apache/2.2.29 (Win32) mod_ssl/2.2.29 OpenSSL/1.0.1j PHP/5.4.36
X-Distributor: AHC
X-Powered-By: PHP/5.4.36
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Content-Range, Content-Disposition, Content-Description,*
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

Simply replacing '$this->output->set_header' with 'header' yields:

Code:
HTTP/1.1 200 OK
Date: Tue, 29 Dec 2015 13:27:41 GMT
Server: Apache/2.2.29 (Win32) mod_ssl/2.2.29 OpenSSL/1.0.1j PHP/5.4.36
X-Distributor: AHC
X-Powered-By: PHP/5.4.36
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Content-Range, Content-Disposition, Content-Description,*
Expires: 0
Cache-Control: must-revalidate
Pragma: public
Content-Description: File Transfer
Content-Disposition: attachment; filename="Segmentation 2015-12-29 16:27:42.txt"
Content-Length: 39132
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/octet-stream

At least 5-6 lines are different, inc. Pragma, Content-Type, missing filename, etc, but most importantly second example works when first - doesn't!


Thanks for Download Helper, I'll try it. Smile
Reply
#4

Download Helper works well, btw. Here is what I received:

Code:
HTTP/1.1 200 OK
Date: Tue, 29 Dec 2015 13:45:38 GMT
Server: Apache/2.2.29 (Win32) mod_ssl/2.2.29 OpenSSL/1.0.1j PHP/5.4.36
X-Distributor: AHC
X-Powered-By: PHP/5.4.36
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Content-Range, Content-Disposition, Content-Description,*
Expires: 0
Cache-Control: private, no-transform, no-store, must-revalidate
Pragma: no-cache
Content-Disposition: attachment; filename="Segmentation 2015-12-29 16:45:39.txt"
Content-Transfer-Encoding: binary
Content-Length: 39132
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/octet-stream
Reply
#5

It's not a bug. You just need to learn how to use the Output class. Look at http://www.codeigniter.com/user_guide/li...utput.html, there's code example. You need to call _display() before you call exit.

PHP Code:
$response = array('status' => 'OK');

$this->output
        
->set_status_header(200)
 
       ->set_content_type('application/json''utf-8')
 
       ->set_output(json_encode($responseJSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES))
 
       ->_display();
exit; 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB