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.
Related
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);
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.
I have to upload a base64 encoded image that i am receiving from android application. I am using php codeigniter framework.
While searching through the forum, the question at this link How to upload base64encoded image in codeigniter is same as mine, but the solution there is not working for me.
Here is the code that i have written:
private function _save_image() {
$image = base64_decode($_POST['imageString']);
#setting the configuration values for saving the image
$config['upload_path'] = FCPATH . 'path_to_image_folder';
$config['file_name'] = 'my_image'.$_POST['imageType'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if($this->upload->do_upload($image)) {
$arr_image_info = $this->upload->data();
return ($arr_image_info['full_path']);
}
else {
echo $this->upload->display_errors();
die();
}
}
I am getting "you did not select a file to upload"
Thanks for your time.
The error is occurring because codeigniter's upload library will look into the $_FILES superglobal to and search for a index you give it at the do_upload() call.
Furthermore (at least in version 2.1.2) even if you would set up the $_FILES superglobal to mimic the behaviour of a file upload it wouldn't pass because the upload library uses is_uploaded_file to detect exacly that kind of tampering with superglobals. You can trace the code in system/libraries/Upload.php:134
I'm afraid that you will have to re-implement size checking and file renaming and moving (I would do this) or you can modify codeigniter to omit that check, but it could make upgrading the framework later difficult.
Save the $image variable's content to a temporary file, and set up the $_FILES to look like this:
$temp_file_path = tempnam(sys_get_temp_dir(), 'androidtempimage'); // might not work on some systems, specify your temp path if system temp dir is not writeable
file_put_contents($temp_file_path, base64_decode($_POST['imageString']));
$image_info = getimagesize($temp_file_path);
$_FILES['userfile'] = array(
'name' => uniqid().'.'.preg_replace('!\w+/!', '', $image_info['mime']),
'tmp_name' => $temp_file_path,
'size' => filesize($temp_file_path),
'error' => UPLOAD_ERR_OK,
'type' => $image_info['mime'],
);
Modify the upload library. You can use codeigniter's built in way of Extending Native Libraries, and define a My_Upload (or your prefix) class, copy-paste the do_upload function and change the following lines:
public function do_upload($field = 'userfile')
to:
public function do_upload($field = 'userfile', $fake_upload = false)
and the:
if ( ! is_uploaded_file($_FILES[$field]['tmp_name']) )
to:
if ( ! is_uploaded_file($_FILES[$field]['tmp_name']) && !$fake_upload )
and in your controller, call do_upload() with the flowing parameters:
$this->upload->do_upload('userfile', true);
You are aware, that if you are receiving an Base64 encoded image, as a string, then you do not need to use the Upload class.
Instead, you just need to decode it using base64_decode and then use fwrite/file_put_contents to save the decoded data...
$img = imagecreatefromstring(base64_decode($string));
if($img != false)
{
imagejpeg($img, '/path/to/new/image.jpg');
}
Credit: http://board.phpbuilder.com/showthread.php?10359450-RESOLVED-Saving-Base64-image.
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>
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));
}