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

#11
[eluser]Sudz[/eluser]
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

#12
[eluser]toopay[/eluser]
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

#13
[eluser]toopay[/eluser]
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).

#14
[eluser]Sudz[/eluser]
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.

#15
[eluser]toopay[/eluser]
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...

Code:
/* [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();
      ob_start();
      //...do something else
      $buffer = ob_get_contents();
      ob_end_clean();
      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
        $zip->addFromString("leads/export.xls",$file_name);
        $zip->close();
        //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');
        readfile($archive_file_name);
        exit;
        */
        // Here you can use CI Download helper
        $this->CI->load->helper('download');
        $data = file_get_contents("/path/to/your_file.zip"); // Read the file's contents
        $name = 'download_somefile.zip';
        force_download($name, $data);
   }
}

#16
[eluser]Sudz[/eluser]
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.