I have this error, Fatal error: Call to undefined method CI_Input::file() in D:\xampp\htdocs\application\controllers\page.php on line 219 and use from Multiple Uploads with JQuery and Code Igniter and Extended Input for Files
This is my code:
if($this->input->file('userfile')){ //This is line 219
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
$this->load->library('Multi_upload');
$files = $this->multi_upload->go_upload();
//var_dump($files);
if ( ! $files )
{
$error = $this->upload->display_errors();
echo $error;
return false;
}
else
{
$data3 = array();
foreach ($files as $idx => $name) {
//var_dump($name['name']);
$data3[] = array(
'relation' => $id_residence,
'images' => $name['name'],
);
};
$data333 = $this->db->insert_batch('hotel_image', $data3);
}
}
Instead of isset()
try:
$myInput = $this->input->file('userfile');
if(!empty($myInput)){
...
...
}
Related
I am uploading multiple images in codeigniter framework. When i select multiple images, it upload only last selected image.
Below is my controller code, where i am doing wrong?
$files = $_FILES;
for($i=0; $i< count($_FILES['item_image']['name']); $i++)
{
$_FILES['item_image']['name']= $files['item_image']['name'][$i];
$_FILES['item_image']['type']= $files['item_image']['type'][$i];
$_FILES['item_image']['tmp_name']= $files['item_image']['tmp_name'][$i];
$_FILES['item_image']['error']= $files['item_image']['error'][$i];
$_FILES['item_image']['size']= $files['item_image']['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('item_image'))
{
$file_name=$this->upload->data('file_name');
$newdata = array(
'item_id'=>$last_id,
'image_name'=>$file_name
);
$this->Item_model->add_item_image($newdata);
$newdatatwo = array(
'item_image'=>$file_name
);
$this->Item_model->update_item($newdatatwo,$last_id);
$data['error_or_success_message'] = $this->session->set_flashdata('error_or_success_message', 'Item added Successfully!');
}else{
$data['error_or_success_message'] = $this->session->set_flashdata('error_or_success_message', 'Some Error, Please Try Again!');
}
}
The second condition in your for loop gets redefined in the first line of your for loop. I would check to make sure that isn't causing errors.
Refer The Below Code
Controller Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Multiple_upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function index()
{
$this->load->view('upload');
}
public function Upload()
{
$files = $_FILES;
$config['upload_path'] = 'common_assets/images';
$config['allowed_types'] = 'jpg|gif|png';
$config['max_size'] = '';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['max_width'] = '';
$config['max_height'] = '';
$config['max_filename'] = 0;
foreach ($_FILES['item_image']['name'] as $key => $value)
{
$_FILES['item_image']['name']= $files['item_image']['name'][$key];
$_FILES['item_image']['type']= $files['item_image']['type'][$key];
$_FILES['item_image']['tmp_name']= $files['item_image']['tmp_name'][$key];
$_FILES['item_image']['error']= $files['item_image']['error'][$key];
$_FILES['item_image']['size']= $files['item_image']['size'][$key];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!empty($value))
{
if (!$this->upload->do_upload('item_image'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
exit();
}else{
$file_name=$this->upload->data('file_name');
//**********Database Entry*********************
// $newdata = array(
// 'item_id'=>$last_id,
// 'image_name'=>$file_name
// );
// $this->Item_model->add_item_image($newdata);
// $newdatatwo = array(
// 'item_image'=>$file_name
// );
// $this->Item_model->update_item($newdatatwo,$last_id);
}
}
}
echo "<pre>Uploaded Successfully"; die;
}
}
?>
//This works for me:
$config['upload_path'] = 'common_assets/images';
$config['allowed_types'] = 'jpg|gif|png';
$config['max_size'] = '';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['max_width'] = '';
$config['max_height'] = '';
$config['max_filename'] = 0;
$this->load->library('upload', $config);
foreach ($_FILES as $fieldname => $fileObject) {
if (!empty($fileObject['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname)) {
$errors = $this->upload->display_errors();
redirect('error_page', $errors);
} else {
$data = array($fieldname => $this->upload->data());
extract($data[$fieldname]);
$$fieldname = $file_name;
}
}
}
$array_to_save = array('tbl_name_field_1' => $file_name_from_post_1,
'tbl_name_field_2' => $file_name_from_post_2);
Hello i need help below is my code
It doesn't update any think except the avatar always display error You did not select a file to upload.
don't know how to solve it please help
if($this->input->post()){
$this->form_validation->set_rules('avatar','avatar','callback_multiple_image_upload|trim');
$this->form_validation->set_rules('overview','overview','trim');
$this->form_validation->set_rules('history','history','trim');
$this->form_validation->set_rules('education','education','trim');
$this->form_validation->set_rules('joining','joining','trim');
$this->form_validation->set_rules('hobbies','hobbies','trim');
$this->form_validation->set_rules('experience','experience','trim');
$this->form_validation->set_rules('skills','skills','trim');
/* if($this->input->post('avatar')){
$this->form_validation->set_rules('avatar','avatar','callback_multiple_image_upload|trim');
}else{} */
if($this->form_validation->run())
{
// check if avatar is uploaded
$images = ($this->session->userdata('uploaded_images'))?$this->session->userdata('uploaded_images'):array();
if($this->session->userdata('uploaded_avatar')){
$user['avatar'] = $this->session->userdata('uploaded_avatar');
$this->session->unset_userdata('uploaded_avatar');
}
$data = array(
'avatar' => ($images)?$images[0]['file_name']:'',
'thumb' => ($images)?$images[0]['thumb']:'',
'overview' =>$this->input->post('overview'),
'history' =>$this->input->post('history'),
'education' =>$this->input->post('education'),
'joining' =>$this->input->post('joining'),
'hobbies' =>$this->input->post('hobbies'),
'experience' =>$this->input->post('experience'),
'skills' =>$this->input->post('skills'),
);
$abc = $this->user_model->update($user_id,$data,'user_id');
$this->session->set_flashdata('success','User updated successfully');
redirect('admin/staff/edit/'.$user_id);
}
}
public function multiple_image_upload()
{
$config = array();
$config['upload_path'] = './uploads/user_avatars/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$config['max_width'] = 10240;
$config['max_height'] = 7680;
//$images = array();
if(isset($_FILES['avatar']) && ($count = count($_FILES['avatar']['name'])) > 0)
{
$files = $_FILES;
$images = array();
/* check if folder with year exists*/
$current_year = date('Y');
$path = './uploads/user_avatars/'.$current_year;
if(is_dir($path)){
/* do nothing */
}else{
/* create directory */
#mkdir( $path , 0755, true );
}
$images = array();
for($i = 0; $i < $count; $i++)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100000';
$config['max_width'] = '102400';
$config['max_height'] = '76800';
$this->load->library('upload', $config);
if ($this->upload->do_upload('avatar'))
{
$data = $this->upload->data();
/* create thumbnail */
$this->load->helper('image_thumb_helper');
generate_image_thumb($data['full_path'],$path,$data['file_name']);
/* add to database */
$images[$i]['file_name'] = $current_year.'/'.$data['file_name'];
$images[$i]['thumb'] = $current_year.'/thumb_'.$data['file_name'];
}else{
$this->form_validation->set_message('multiple_image_upload',$this->upload->display_errors());
return false;
}
}
$this->session->set_userdata('uploaded_images',$images);
return true;
}
else{
return true;
}
}
please provide me solution m not able to remove this error :(
I thing you are using multifile upload. please try bellow one for "for loop"
for($i = 0; $i < $count; $i++)
{
$_FILES['avatar_solo']['name'] = $_FILES['avatar']['name'][$i];
$_FILES['avatar_solo']['type'] = $_FILES['avatar']['type'][$i];
$_FILES['avatar_solo']['tmp_name'] = $_FILES['avatar']['tmp_name'][$i];
$_FILES['avatar_solo']['error'] = $_FILES['avatar']['error'][$i];
$_FILES['avatar_solo']['size'] = $_FILES['avatar']['size'][$i];
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100000';
$config['max_width'] = '102400';
$config['max_height'] = '76800';
$this->load->library('upload', $config);
if ($this->upload->do_upload('avatar_solo'))
{
$data = $this->upload->data();
/* create thumbnail */
$this->load->helper('image_thumb_helper');
generate_image_thumb($data['full_path'],$path,$data['file_name']);
/* add to database */
$images[$i]['file_name'] = $current_year.'/'.$data['file_name'];
$images[$i]['thumb'] = $current_year.'/thumb_'.$data['file_name'];
}else{
$this->form_validation->set_message('multiple_image_upload',$this->upload->display_errors());
return false;
}
}
I need to upload pictures and data create dynamically html and jQuery I use codeigneter framework and send the the all data to mysql.
This is code controller
public function Addtest() {
$this->load->library('upload');
$u= $this->session->userdata('id_user');
$files = $_FILES;
$cpt = count($_FILES['sliderimg']['name']);
$inputAll= $this->input->post();
$this->load->model('AddData');
for($i=0; $i < count($inputAll['artilce']); $i++) {
$_FILES['sliderimg']['name']= $files['sliderimg']['name'][$i];
$_FILES['sliderimg']['type']= $files['sliderimg']['type'][$i];
// $_FILES['sliderimg']['tmp_name']= $files['sliderimg']['tmp_name'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
var_dump($_FILES);
$data= array(
'id_user'=>$u,
'description2'=>$inputAll['description2'][$i],
'description1'=>$inputAll['description1'][$i],
'art'=>$inputAll['artilce'][$i],
$_FILES['sliderimg']['name']= $files['sliderimg']['name'][$i],
$image_name =>$_FILES['sliderimg']['name'][$i],
);
if( $this->AddData->Add_data($data)) {
echo 'false';
} else {
echo 'true';
}
and this the model code
public function Add_data($artt) {
extract($artt);
$data = array(
'Art'=>$art,
'des1'=>$description1,
'desc2'=>$description2,
'picture'=>$image_name,
'id_foreign'=>$id_user,
);
$this->db->insert('table',$data);
}
and the this the error output while I try to upload 2 pictures and data
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: image_name
and this is the set_upload_options() function
private function set_upload_options()
{
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000';
$config['max_width'] = '20000';
$config['max_height'] = '17680';
$config['overwrite'] = FALSE;
return $config;
}
and the do_uplaod:
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000';
$config['max_width'] = '2024';
$config['max_height'] = '1068';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
return "false";
}
else
{
return "true";
}
}
Codeigniter by 3 I am trying to create a controller for multiple uploads.
My vontroller:
public function do_upload($path)
{
$files = $_FILES['file'];
$num_file = count($_FILES['file']['name']);
echo $num_file;
for($i=0; $i<=$num_file; $i++)
{
if(isset($files['name'][$i]))
{
echo $this->session->userdata('dir_corso');
$this->_CI->session->set_userdata(array('filename'=> $files['name'][$i]));
$this->load->library('upload', $this->set_upload_options());
$this->upload->initialize($this->set_upload_options());
chmod($path, 0777);echo '<br/>'.$this->_CI->session->userdata('filename');
$this->upload->do_upload($files['name'][$i]);
$error = array('error' => $this->upload->display_errors());
foreach($error as $errore) echo $errore;
public function set_upload_options(){
$config['upload_path'] = $this->session->userdata('dir_corso');
$config['allowed_types'] = 'gif|jpg|png|GIF|JPG|PNG';
$config['overwrite'] = TRUE;
if(!empty($this->session->userdata('filename'))){
$config['file_name'] = $this->session->userdata('filename');
}
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config ['encrypt_name'] = TRUE;
return $config; }}}
I'm not getting any error.
Why do not upload files?
Help me.
Function inside another function?? hope that is the issue here,
Try this,
public function do_upload($path) {
$files = $_FILES['file'];
$num_file = count($_FILES['file']['name']);
echo $num_file;
for ($i = 0; $i <= $num_file; $i++) {
if (isset($files['name'][$i])) {
echo $this->session->userdata('dir_corso');
$this->_CI->session->set_userdata(array('filename' => $files['name'][$i]));
$this->load->library('upload', $this->set_upload_options());
$this->upload->initialize($this->set_upload_options());
chmod($path, 0777);
echo '<br/>' . $this->_CI->session->userdata('filename');
$this->upload->do_upload($files['name'][$i]);
$error = array('error' => $this->upload->display_errors());
foreach ($error as $errore)
echo $errore;
}
}
}
public function set_upload_options() {
$config['upload_path'] = $this->session->userdata('dir_corso');
$config['allowed_types'] = 'gif|jpg|png|GIF|JPG|PNG';
$config['overwrite'] = TRUE;
if (!empty($this->session->userdata('filename'))) {
$config['file_name'] = $this->session->userdata('filename');
}
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config ['encrypt_name'] = TRUE;
return $config;
}
Hi i have this file upload of images, and i want to do with a multiple images. Ive been searching in google and i found any no luck at all. I have this code <input type="file" name="photo[]" multiple="" /> and in my controller here
echo ("<pre>");
print_r($_FILES);
echo ("</pre>s");
$name_array = array();
$count = count($_FILES['photo']['size']);
$ctr = 0;
if ($_FILES){
$files = $_FILES['photo'];
foreach($_FILES['photo']['name'] as $key=>$value){
$_FILES['photo']['name']= $files['name'][$ctr];
$_FILES['photo']['type'] = $files['type'][$ctr];
$_FILES['photo']['tmp_name'] = $files['tmp_name'][$ctr];
$_FILES['photo']['error'] = $files['error'][$ctr];
$_FILES['photo']['size'] = $files['size'][$ctr];
echo ("<pre> NEW >>> " . $ctr);
print_r($_FILES);
echo ("</pre>");
$ctr++;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
print_r($data);
$name_array[] = $data['file_name'];
$names= implode(',', $name_array);
/* $this->load->database();
$db_data = array('id'=> NULL,
'name'=> $names);
$this->db->insert('testtable',$db_data);
*/ print_r($names);
}
}
my action <?php echo form_open_multipart('post/add') ?>
and what ive got the error is this way
A PHP Error was encountered
Severity: Warning
Message: is_uploaded_file() expects parameter 1 to be string, array given
Filename: libraries/Upload.php
Line Number: 161
You did not select a file to upload.
Can someone help me figured this thing out? on how multiple uploads will do in codeigniter?? Any help is muchly appreciated. Thanks
controller:
class Imageupload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('imageupload_view', array('error' => ' ' ));
}
function doupload() {
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++) {
$_FILES['userfile']['name']=$value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
$names= implode(',', $name_array);
/* $this->load->database();
$db_data = array('id'=> NULL,
'name'=> $names);
$this->db->insert('testtable',$db_data);
*/ print_r($names);
}
}
view:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('imageupload/doupload');?>
<input name="userfile[]" id="userfile" type="file" multiple="" />
<input type="submit" value="upload" />
<?php echo form_close() ?>
</body>
</html>
Try this:
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['photo']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['photo']['name']= $files['photo']['name'][$i];
$_FILES['photo']['type']= $files['photo']['type'][$i];
$_FILES['photo']['tmp_name']= $files['photo']['tmp_name'][$i];
$_FILES['photo']['error']= $files['photo']['error'][$i];
$_FILES['photo']['size']= $files['photo']['size'][$i];
$config['upload_path'] = './uploads/';
....
....
$this->upload->initialize($config);
$this->upload->do_upload();
}