2
votes

I have been getting a warning error all day yesterday and can't get it off. Don't know what is happening. it is complaining that some output was sent before execution of a redirect on property_model.php on line 1. I went there and checked for everything but no luck. I also checked for whitespaces. I am going to paste down the error and code.

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/sddhd/public_html/rsw/application/models/property_model.php:1)

Filename: helpers/url_helper.php

Line Number: 542

Now the code of model it shows above is:

<? class Property_model extends CI_Model {
    function __construct()
        {
            parent::__construct();
        }

        function insert_property($propertyID)
        {
            $this->property_id = $propertyID;
            $this->type = $_POST['property_type'];
            $this->city = $_POST['property_city'];
            $this->address = $_POST['property_address'];
            $this->description = $_POST['property_description'];
            $this->owner = $_POST['property_owner'];
            $this->date_added = date("Y-m-d");
            $this->price = $_POST['property_price'];
            $this->offer_type = $_POST['property_offer'];
            $this->contact = $_POST['property_contact'];
            $this->db->insert('property', $this);
        }

        function insert_image_details($id, $url, $extension)
        {
            $current->property_id = $id;
            $current->extension = $extension;
            $current->url = $url;
            $this->db->insert('photos', $current);
        }
    }
?>

and the uses of the redirect call (including whole controller call making calls to the model):

<?php
class Property extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
                $this->load->helper('url');
        $this->load->model('property_model', 'propertymodel');
        $this->load->database();
    }

    public function index() {
        $this->load->view('home');
    }

    public function new_property()
    {
        $this->load->view('new_property', array('error' => '' ));    
    }

    public function create_property()
    {
        //Pass the posted data to model for insertion
        $propertyID = uniqid();
        $this->propertymodel->insert_property($propertyID); 
        $this->do_upload($propertyID);
        redirect('/home');
    }


        public function do_upload($propertyId)
        {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size'] = '1000';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

            $this->load->library('upload', $config);
                if ( ! $this->upload->do_upload())
                {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('create_property', $error);
                }
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    //array to access all the file uploaded attributes
                    $image_data = $this->upload->data();

                    //parameters to be passed to model: 
                    $imgUrl = "../.".$config['upload_path'].$image_data['file_name'];
                    //$imgTitle = $image_data['file_name'];
                    $fileExtension = $image_data['file_ext'];
                    $this->propertymodel->insert_image_details($propertyId, $imgUrl, $fileExtension);
                }           
        }//end of function


}
?>

Any help would be greatly appreciated

5
Check for any whitespace before '<?' or after '?>' on property_model.php - Dr. Dan
I did that couple of times. Nothing there. I also viewed it on multiple editors just in case - sys_debug
just to be safer side, use <?php instead of <?. Not related to your problem may be but still worth to mention. there may be a white space there btw. - itachi
I added <?php but still nothing. As I said, not a whitespace because I checked for those near opening and closing - sys_debug
As you said it's not a whitespace issue, but it's also good practice not to close out PHP only pages. Just leave the ?> off the end. It's not necessary and can save you whitespace problems in the future. - Rick Calder

5 Answers

3
votes

I had the same problem. Nothing helped. Then I decided to create a complete new file and copied the content from the file that caused the error into the new one. This worked! Apparently the original file had a hidden byte at the position 0.

2
votes

You have mistake in Property controller. replace these two lines in the method create_property

$url= base_url().'home';
redirect($url);

with

redirect('/home');
0
votes

Have you enabled error_reporting? The model might throw an error you don't see here. And this error causes the already sent headers.

By the way, where does $current in the method insert_image_details come from? That'll probably throw an error.

0
votes

Might be some warning getting sent before the actual page is being load. Please turn on the error reporting using following functions in the index.php of your document root.

ini_set('display_errors', 'On');
error_reporting(E_ALL);
0
votes

Found this alternative solution posted by another person in Stackoverflow.

 //Alternate Code for solve this issue
    $url = 'site/function1';
    echo'
    <script>
    window.location.href = "'.base_url().'index.php?/'.$url.'";
    </script>
    ';

This works really well! Hope there are no disadvantages to using this! Thanks all for support.

Codeigniter: headers already sent error