CodeIgniter doesn't load upload library - 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.

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 upload file redirects to unknown link

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

Grocery Crud - Set the allowed file types in upload field

My question is similar to this one http://www.grocerycrud.com/forums/topic/169-allowed-types-for-file-upload/
The answer there is no longer updated.
how can I set allowed types for file upload in grocery crud with the version 1.4?
Could the file types be set directly from the function? Something like this one:
$crud->set_upload_file_types('jpg','apk');
Thanks
At this moment you can not change it directly from the controller function.
But you can change it from the config file at your application/config/grocery_crud.php
$config['grocery_crud_file_upload_allow_file_types'] = 'gif|jpeg|jpg|png|tiff|doc|docx|txt|odt|xls|xlsx|pdf|ppt|pptx|pps|ppsx|mp3|m4a|ogg|wav|mp4|m4v|mov|wmv|flv|avi|mpg|ogv|3gp|3g2|apk';
I had the same issue (i could easily do what Jawaad said, but i needed to define file types for each field). Here is my solution:
https://github.com/scoumbourdis/grocery-crud/pull/290/files
Sent a pull request to GroceryCrud, if they choose to use it or not is upto them.
Refer to the commit msg for instructions (https://github.com/scoumbourdis/grocery-crud/pull/290)
i tried another solution and it is working
$crud->set_field_upload("image","assets/uploads/team","jpg|png");
it just to add the allowed file types like in my code separated by '|'
Works 100% in Grocery Crud and Codeigniter version 3
you'd configure your
in the folder
application/config/grocery_crud.php
$config['grocery_crud_file_upload_allow_file_types'] = 'gif|jpeg|jpg|png|tiff|doc|docx|txt|odt|xls|xlsx|pdf|ppt|pptx|pps|ppsx|mp3|m4a|ogg|wav|mp4|m4v|mov|wmv|flv|avi|mpg|ogv|3gp|3g2|apk';
With $crud->set_field_upload("image","assets/uploads/team","jpg|png"); you cant overide at all , it picks extension from above grocery_crud.php.
Finally in your controller ,this is my example :
function __construct() {
parent::__construct();
$this->load->driver('session');
$this->load->database();
$this->load->helper('url');
$this->load->library('grocery_CRUD');
$this->load->model('Generic');
$this->load->library('upload');
$this->load->helper(array('form','url'));
//$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|rar|zip';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
}
public function index() {
$crud = new grocery_CRUD();
...
//dondt forget to set some configuration of visibily from your crud pages where you want your Upload Input appears like...
$crud->fields("file_url" ...
$crud->add_fields("file_url"..
$crud->edit_fields("file_url"..
....
$crud->set_field_upload('file_url','assets/uploads/files');
$output = $crud->render();
....
}

Why is my upload path invalid? (Codeigniter-Library)

I'm creating a library as part of a project and one of the methods is a wrapper for the upload helper.
The method:
public function upload(){
echo "Doing upload";
$config['upload_path']= RESOURCE_PATH . "Downloads";
$config['allowed_types']='pdf|doc';
$config['max_size']='10000';
//echo $config['upload_path'];
$this->CI->load->library('upload',$config);
if(!$this->CI->upload->do_upload()){
echo "Couldn't do the upload";
echo $this->CI->upload->display_errors();
echo $config['upload_path'];
}
else{
echo "Could do the upload";
}
}
I've checked the directory permissions of the Downloads folder and that it exists but I'm getting the following error: "The upload path does not appear to be valid."
How do I resolve this issue?
EDIT:: I created a symlink so my directory strucutre really looks like:
www -> /home/user/Dropbox/www/appname
Note about variables used:
$this->CI = &get_instance(); // Defined in custom library class
define('RESOURCE_PATH', APPPATH . 'views/resources/'); // Defined in constants.php
Figured out the idiotic problem.
I was autoloading the library and some how when I was trying to
initialize the configuration by $this->load->library('upload',
$config); it wouldn't do so.
Instead I put my config files in config/upload.php
The other method to do so would have been
$this->upload->initialize($config);
See this answer.
Add this line
$this->CI->upload->initialize($config);
after
$this->CI->load->library('upload',$config);

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.

Categories