• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot modify header information - headers already sent

I tried man,
But still giving the same error

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent

Filename: helpers/my_userExel_helper.php

Line Number: 494

I don't know your whole library looks like and how its flow process. Also is there are loop or nested loop inside your library.

You can working around it with ob_clean(), ob_flush(), proper validation(headers_sent()), save the header in some array variable(apache_response_headers()), and so on. Just make sure you send the header before send out the buffer

In addition, you may look at CI core class, and make sure your ob_start(), ob_end_clean() didnt crash with them (as far as i remember, CI also use ob_start() and ob_clean(), built in, in their core class, and they maybe will give you problems too).

Initially i send the buffer to read it contents and write zip file,
and then i send the header to force download.
This is what i am trying to do.

Since CI also use ob_start() and ob_end_clean(), so that If you use those, you should provide proper validation (which i'm not see so far in your code, as i said above).

And i've just notice, that you write this on helper file instead library file. I suggest you modify your helper into some Library/Class, so you can do more proper validation, or even use download helper, something like...

/* [NOT TESTED] */
class userExel{

   protected $ob_level;
   protected $CI;

   public function __construct()
       // Set the super object to a local variable for use later
       $this->CI =& get_instance();
       $this->ob_level = ob_get_level();
       log_message('debug', "User Excell Class Initialized");

   //... now, since you declared this as class, you can do proper validation like this
   function somefunction($some_var)
      if (ob_get_level() > $this->ob_level + 1) ob_end_flush();
      //...do something else
      $buffer = ob_get_contents();
      return $buffer;

   //... the rest of your code
   function zipFilesAndDownload($file_name,$archive_file_name)
        //create the object
        $zip = new ZipArchive();

        //create the file and throw the error if unsuccessful
        if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE)
            exit("cannot open <$archive_file_name>\n");

        //add each files of $file_name array to archive
        //then send the headers to force download the zip file
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=$archive_file_name");
        header('Cache-Control: max-age=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        // Here you can use CI Download helper
        $data = file_get_contents("/path/to/your_file.zip"); // Read the file's contents
        $name = 'download_somefile.zip';
        force_download($name, $data);

Thanks toopay,
I got my mistake,
I was sending to buffer output prior to sending header that's why.
Thanks for your above code I converted my helper to library and it works fine.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.