codeigniter whitespace or header causing warning - php

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

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.

You have mistake in Property controller.
replace these two lines in the method create_property
$url= base_url().'home';
redirect($url);
with
redirect('/home');

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.

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);

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

Related

Codeigniter file upload code not working

Controller Code:
if(isset($_FILES['imageupload']) && $_FILES['imageupload']['size'] > 0){
echo 'inside if';
if ( ! $this->upload->do_upload('imageupload'))
{
echo 'inside if';
$error = array('error' => $this->upload->display_errors());
}
else
{
echo 'inside else';
$upload_data = $this->upload->data();
$file_name[] = $upload_data['file_name'];
}
}
View Code:
<input type="file" class="default" id="imageupload" name="imageupload">
ERROR: Call to a member function do_upload() on a non-object
Hoow can I resolve this error please help me.
Did you load the library upload on your controller? I guess the problem of your code is lacking of this code $this->load->library('upload', $config); try to check this documentation hope it will help you. https://www.codeigniter.com/userguide3/libraries/file_uploading.html
The error indicates that the upload library is not loaded. You need to load the upload library somewhere and in the constructor of the controller is one possible choice. Add the following to the controller code.
function __construct()
{
parent::__construct();
$this->load->library('upload');
}
The other place to load this library is in application/config/autoload.php using the $autoload['libraries'] item.
$autoload['libraries'] = array('upload');
Other libraries can be loaded at the same time. See the comments in autoload.php for more details.
Add upload library before you could call do_upload with config
$config['upload_path'] = '/path/';
$config['allowed_types'] = '*';
$config['file_name'] = 'file_name';
$config['overwrite'] = TRUE|FALSE;
$this->load->library('upload', $config);

CodeIgniter doesn't load upload library

I'm having a problem with loading a CodeIgniter library,
I've already built three (3) websites with CodeIgniter, so I consider myself familiar with this framework.
The problem is only with the upload lib. I've tried with multiple functions and it still doesn't work.
I've even tried with the CodeIgniter example
In google I can't find any answers, does anyone have an idea what it could be?
class Admin extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->library('v');
$this->load->model('admin_model');
}
public function upload_a_thing($set = null){
$this->load->helper(array('form', 'url'));
if(!$set){
$this->load->view('admin/upload_a_thing');
}else{
$this->load->library('Upload');
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
} else {
$file = $this->upload->data();
// $product = $this->admin_model->get_products($id);
$newFilePath = $config['upload_path'].'try.jpg';
$file['file_name'] = str_replace(" ","_",$file['file_name']);
rename($config['upload_path'].$file['file_name'], $newFilePath);
}
}
}
CodeIgniter Error
Undefined property: Admin::$upload
PHP error
Fatal error: Call to a member function do_upload() on a non-object
Edited for Spary
$config['upload_path'] = './media/imgs/products/';
$this->load->library('upload',$config);
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
}
If your form looks like this:
<input type="file" class="input" name="logo" />
then call do_upload() like this:
do_upload('logo');
If it looks like this:
<input type="file" class="input" name="userfile" />
then call it like this:
do_upload();
do something like this
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
}
Your code...
$this->load->library('Upload');
As per all examples in the documentation, upload should be in all lower-case and most likely the reason the library is not loading.
Additionally, if your $config array is missing, you're not following the example code in the documentation:
$config['upload_path'] = './uploads/'; // <- preferences
$this->load->library('upload', $config); // <- load library and set configuration
OR
$this->load->library('upload'); // <- load library
$config['upload_path'] = './uploads/'; // <- preferences
$this->upload->initialize($config); // <- set configuration
OR
// 'upload' library is "auto-loaded" // <- load library
$config['upload_path'] = './uploads/'; // <- preferences
$this->upload->initialize($config); // <- set configuration
From the chart of preferences, they all seem optional, except for upload_path...
Default Value: None
Description: The path to the folder where the upload should be placed. The folder must be writable and the path can be absolute or relative.
I'm not sure how CodeIgniter could know where to upload the file when there's no upload_path because there is no default and your entire configuration array is missing.

Creating a multiple file upload form with CI

hope someone can help me with this.
I am developing a form which allows to upload multiple files using CodeIgniter and jQuery to enable multiple file uploads using one uploadfield.
But now I get the following errmsg in firebug:
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.
Which means that the HTML-document is not decleared but I don't understand what is going wrong.
I have the following controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends CI_Controller {
function Upload(){
parent::Controller();
$this->load->helper(array('form', 'url'));
}
public function index(){
$this->load->view('includes/header');
$this->load->view('upload');
$this->load->view('includes/footer');
}
function do_upload()
{
$config['upload_path'] = './uploads/'; // server directory
$config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
$config['max_size'] = '1000'; // in kb
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('Multi_upload');
$files = $this->multi_upload->go_upload();
if ( ! $files )
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $files);
$this->load->view('upload_success', $data);
}
}
}
So my first thought is that I have not decleared the Meta-tag in the header but that is not the case:
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<link href="<?=base_url();?>src/css/style.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-1.8.0.js"></script>
<script src="<?=base_url();?>src/js/jquery.MultiFile.js"></script>
</head>
And if I remove the Upload-function from the controller I get an error of calling an undefined method form_open_multipart
So I have no idea why I get the errmsg I first stated. Is there someone who can help me with this?
Thanks in advance
Use this plugin http://www.plupload.com/example_queuewidget.php ...Its works well for me so many times.

codeigniter photo upload no error warning on non allowed types files

Here is upload function:
$config['upload_path'] = './photos/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024 * 2;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
//$this->upload->initialize($config);
if (!$this->upload->do_upload("photo_data"))
{
$this->error = true;
$this->response = $this->upload->display_errors('', '');
}
else
{
$this->upload->data();
$this->response = "Photo successfully changed.";
}
$array = array(
'error' => $this->error,
'response' => $this->response
);
return $array;
However in order to test that it doesnt allow file types except the one that i allow i renamed a video to .jpg and tried to upload it...
It didnt proceed but it didnt also send an error message... i suppose somwhere in the upload class returns false... any idea how to make sure it sends a message to the user first?
How are you printing the errors in your view? The method returning false somewhere in the upload class is the one you already checked for FALSE, i.e. $this->upload->do_upload()
You should pass those errors to a view, if you want the site to display them:
This assumes the code you posted is from a controller:
$array = array(
'error' => $this->error,
'response' => $this->response
);
$this->load->view('upload_view',$array);
//instead of returning, as "return" in a controller makes little sense. If you're inside a model, then, get the returned value in a controller and pass it to a view.
In your "upload_view.php" view file:
<div class="<?php $error? 'error' : 'success';?>"><?php echo $response;?></div>

setting $this->data in a method then gets unset

I am uploading an image with codeigniter, and in the method I am doing the following,
if(isset($_FILES['product_image']['name'])) {
//some setup needed
$config['upload_path'] = "./media/images/products";
$config['allowed_types'] = "png|gif|jpg|jpeg";
$config['max_width'] = 1490;
$config['max_height'] = 400;
//make sure the library is running with clean config
$this->upload->initialize($config);
//do the upload
if(!$this->upload->do_upload('product_image')){
$this->data['image_error'] = $this->upload->display_errors();
$this->template->build('/admin/products/create', $this->data);
} else {
$this->data['image_data'] = $this->upload->data();
//die(print_r($this->data));
$this->template->build('/admin/products/create', $this->data);
}
}
so basically I check to see if there is something in the $_FILES and then upload if there is assigning $this-data['image_data'] with the upload data along the way. However when I come to process the data i.e. save the filename in a database, I cannot access $this->data['image_data'] below is how I am trying to use it,
if($this->input->post("submit_create") == "Save") {
die(print_r($this->data['image_data']));
}
however I get the following error,
Message: Undefined index: image_data
why is this I though assigning things to $this made them accessible not just throughout the method but the entire controller?
I think, when you are printing image_data at this point in your example:
if($this->input->post("submit_create") == "Save") {
die(print_r($this->data['image_data']));
}
You should actually be using $image_data instead:
if($this->input->post("submit_create") == "Save") {
die(print_r($image_data));
}

Categories