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);
Related
I have a complete php script which i bought from codecanyon and the Author does not support costum work... it was made with Codeigniter framework and its working fine and perfectly but the script does not support
Ability for User to Upload Profile Image
Ability for uploaded image be shown in profile page.
3 Ability to ReUpload and or delete image.....
I have search google for it and could not find answers to this.
I have tried Creating >Upload-Controller > Upload View > Upload
Model, which only upload to a specified folder in a specific url e.g site.com/uploadimage.
I want to ask if there is any way to integrate this feature to the system... Am a php newbie.
Many Thank in Advance...
I hope this can help you.
Create this folder path for uploading the image : app_data/images/account/
// Your Controller
Class User extends CI_Controller
{
// This upload method
function upload_user_photo()
{
$path = 'app_data/images/account/';
if(!file_exists($path))
{
mkdir($path, 0777, true);
}
$file_name = increment_string('image_profile', '-'); //use codeigniter string helper
$config['upload_path'] = $path;
$config['file_name'] = $file_name;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//Take an action if photo has been uploaded
if($this->upload->do_upload('image'))
{
$upload_data = $this->upload->data();
// View response from upload data
echo "<pre>";
print_r ($upload_data);
echo "</pre>";
// Array key is column name of your table
$data =
[
'user_id' => 'insert our user id',
'photo' => $upload_data['file_name']
];
//Take action save to your model
$this->load->model('your_model_name');
$this->your_model_name->save_profile_image($data);
}
}
}
#Model
Class Your_model_name extends CI_Model
{
function save_profile_image($data)
{
$this->db->insert($data);
}
}
Read more about codeigniter upload setting preferences
this is my controller
public function upload_file(){
$this->load->helper('form');
$this->load->helper('url');
// set path to store uploaded files
$config['upload_path'] = './uploads/';
// set allowed file types
$config['allowed_types'] = 'pdf';
// set upload limit, set 0 for no limit
//$config['max_size'] = 0;
// load upload library with custom config settings
$this->load->library('upload', $config);
if($this->upload->do_upload()){
$this->add_view();
}else{
print_r('x');
exit();
}
}
and my view is
echo form_open_multipart('PDFuploads/upload');
the problem is i don't know why it redirected into this link
http://localhost/TLC_HR/PDFuploads/upload
Give the correct controller and action here
echo form_open_multipart('controller/action');
and you mentioned your controller action as upload_file()
$this->upload->do_upload('userfile')
userfile is missing. input type = "file" name = "userfile"
Check your 'base_url' in application/config/config.php
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.
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
I'm using Plupload to manage file uploads for my site. When I configure Plupload to post to the following test file, the records are shown correctly, however when I post to a CI controller, both $_POST and $_FILES are empty.
test.php
<?php print_r($_FILES); print_r($_POST); ?>
CI does correctly display the $_FILES and $_POST arrays when using a standard HTML form, so any ideas what's causing this?
EDIT
here is the plupload config
var uploader = new plupload.Uploader({
runtimes : 'html5,html4,flash,silverlight,browserplus',
browse_button : 'pickfiles',
container : 'container',
max_file_size : '15mb',
url : '/test.php',
//url : '/upload/do_upload/',
flash_swf_url : '/js/plupload/plupload.flash.swf',
silverlight_xap_url : '/js/plupload/plupload.silverlight.xap',
filters : [
{title : "Documents", extensions : "pdf,doc,docx,rtf,txt"}
],
multipart_params : { job : -2 }
});
and here is the controller
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
print_r($_POST);
print_r($_FILES);
$config['upload_path'] = 'incoming/';
$config['allowed_types'] = 'pdf|doc|docx|rtf|txt';
$config['max_size'] = '900';
$this->load->library('upload');
$this->upload->initialize($config); // MUST CALL ELSE config not loaded
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->model('translation_model');
$this->translation_model->add_orig($job, $filePath);
$this->load->view('upload_success', $data);
}
}
}
Most probable cause for this is bad mod rewrite rules. If you are using any, try disabling them and post your data again.
I'm using the input class in conjunction with regular $_POST variables and it has always worked for me. The theory that CI cleans out post variables doesn't add up when he can dump them in regular forms.
Turn off csrf protection and please let us know the results. Use firebug console to read the answer from the server.
For a workaround regarding csrf I use https://github.com/EllisLab/CodeIgniter/pull/236
CodeIgniter also has issues with file types.
http://codeigniter.com/forums/viewthread/113029
I have the working libraries for both, if you want them just drop me a message.
First of all, you should check that you have a form tag for enctype.
add enctype="multipart/form-data" to form as it supports uploading.
It will work, can you try and put both the print_r function after else tag before $data,
It will run once the if () condition satisfies. So add it in the else tag and run it.
function do_upload()
{
$config['upload_path'] = 'incoming/';
$config['allowed_types'] = 'pdf|doc|docx|rtf|txt';
$config['max_size'] = '900';
$this->load->library('upload');
$this->upload->initialize($config); // MUST CALL ELSE config not loaded
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
print_r($_POST);
print_r($_FILES);
$data = array('upload_data' => $this->upload->data());
$this->load->model('translation_model');
$this->translation_model->add_orig($job, $filePath);
$this->load->view('upload_success', $data);
}
}
It could be that something in plupload is intercepting $_POST and $_FILES.
In codeigniter it's usually better to use the input class to deal with post variables and the file uploading class to deal with file uploads.
I guess that something has emptied the $_POST and $_FILES array for safety, it could be that on including the input and file uploading classes codeigniter itself wipes the $_POST and $_FILES arrays so that you're forced to use their classes. It's always a good idea as codeigniter removes XSS attacks and cleans up variables.