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.
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
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);
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'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.
I'm building an image uploading script (for the first time) in Codeigniter and the way I have it is that if the image upload form gets validated it performs the following code in a model:
public function upload($id) //function to handle the initial image upload before crop
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = 'images/uploads/temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$file_data = $this->upload->data();
if ( ! $this->upload->do_upload() )//the default parameter of do_upload() is 'userfile' (meaning the name of the form input)
{
$error = '<p>Upload failed!</p> <p>Please make sure the image is in a .jpg, .gif or .png format and that it\'s no larger than 1028 x 768 pixels in dimension. Also it can\'t be more than 2MBs in size. If this problem persists, please contact webmaster#dreamsy.org.</p>';
$this->session->set_flashdata('error', $error);
redirect(base_url().'community/upload_image');
}
else
{
$image_path = base_url().'images/uploads/temp/'.$file_name;
//$data = array( 'upload_data' => $this->upload->data() );
//$this->load->view('community/upload_success');
$this->load->helper('form', 'url');
$vars['id'] = $this->uri->segment(3);
$vars['$image_path'] = $image_path;
$vars['$file_data'] = $file_data;
$this->load->vars($vars);
$this->template->write_view('content', 'community/upload_success');
$this->template->render();
}
}>template->render(); //template library
But when I call the variables that are loaded (via load->vars()) they don't get loaded and when the page loads I get "undefined variable" errors. I suspect it's not possible to pass variables from a model to the view even when the view is loaded from within the model as I have done above. Or maybe I'm just doing something incorrectly (as I'm a bit of a n00b).
Would you even take this route? Would it make more sense to pass the variables to the controller and then to load the view from the controller? Or something else I haven't considered at all? lol
Any help is greatly appreciated. Thanks in advance.
*edit:
I also have the $image_path variable inside of an
Would you even take this route? Would it make more sense to pass the variables to the controller and then to load the view from the controller? Or something else I haven't considered at all?
Yeah, generally speaking it is somewhat accepted to have your views talk directly to the model and vice versa but only when using a presentation layer. In the case of CI, I think it's best to have all the data loaded in from your controller to your view when you render the view like this:
$this->load->view('your-view', $data);
To pass the vars from the model to the controller you can call methods on your model like this:
// Your_Model is the class name of your model
// your_model is the variable name it gets injected into
$this->load->model('Your_Model','your_model');
$data['some_data'] = $this->your_model->get_some_data();
To define those getters in your model, you define a function like this:
function get_some_data()
{
return $this->some_data;
}
That way you create an API into your data, and you can have the getter retrieve whatever you will need in the format you will need it in.
Assuming the line:
$this->load->vars($vars);
is where you want to load your view, then it should be:
$this->load->view('nameofview', $vars);