Hi,
Can anyone help me for uploading files into different folders using Code Igniter.
Right now all files goes into a single folder insted of different folder even I have set path for them in config variable.
Thanks
Hi,
Can anyone help me for uploading files into different folders using Code Igniter.
Right now all files goes into a single folder insted of different folder even I have set path for them in config variable.
Thanks
In CodeIgniter the Upload library is stateful: once loaded, it keeps its config until you reinitialize it. To send different inputs to different folders, reinitialize the library with a new upload_path for each field and call do_upload() with the field name. Also ensure each target directory exists and is writable; upload_path must be a server path (not a URL), and using FCPATH helps build absolute paths.
Example controller logic for three different inputs:
$this->load->library('upload');
$targets = [
'front_thumb' => FCPATH."uploads/shirt/parts/{$fabric_id}/front/thumb_img",
'front_large' => FCPATH."uploads/shirt/parts/{$fabric_id}/front/large_img",
'front_join' => FCPATH."uploads/shirt/parts/{$fabric_id}/front/join_img",
];
$errors = [];
$uploaded = [];
foreach ($targets as $field => $path) {
if (!is_dir($path)) { mkdir($path, 0755, true); }
$config = [
'upload_path' => $path,
'allowed_types' => 'gif|jpg|png',
'max_size' => 2048,
'remove_spaces' => true,
'file_ext_tolower' => true,
'encrypt_name' => true,
];
$this->upload->initialize($config); // reinit per file
if (!$this->upload->do_upload($field)) {
$errors[$field] = $this->upload->display_errors('', '');
} else {
$uploaded[$field] = $this->upload->data();
}
} Common pitfalls:
[] (arrays), loop over each index and map it into $_FILES['userfile'] before calling do_upload('userfile').do_upload() as a global function; use $this->upload->do_upload('field') from a controller method.See the official docs for details on do_upload($field) and configuration options at CodeIgniter Uploading Files.
Jump to Post— mschroeder 251How about you show us the code you're using so we can formulate a solution.
Jump to Post— Stefano Mtangoo 455here is an example from
<?php class Upload extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } function index() { $this->load->view('upload_form', array('error' => ' ' )); } function do_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = …
Jump to Post— Stefano Mtangoo 455This will upload images into a single folder.Needed to upload 3 images of a single form into different folder.
you can do a trick:
function do_upload($path) { $config['upload_path'] = $path; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); if ( ! …
Jump to Post— Stefano Mtangoo 455do_upload($path)<--------------------did you forget $path??
Jump to Post— veedeoo 474I thought this question was just posted some 20+ hours ago, and then I just realized it was awaken from the bone files of two years ago. :).
I think very few people know about Code Igniter :(
How about you show us the code you're using so we can formulate a solution.
Thanks mschroeder, for your reply.
Actually problem is solved with this
$this->load->library('upload', $config2);
$this->upload->initialize($config2);
I done this for each image file while setting path into config variable.
But I am new to CI so dont know how above 2 nd line of code works .
Could you tell me your reply will be preceious for me.
Thanks,
Nalini
here is an example from
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$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);
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->view('upload_success', $data);
}
}
}
?> This will upload images into a single folder.Needed to upload 3 images of a single form into different folder.
This will upload images into a single folder.Needed to upload 3 images of a single form into different folder.
you can do a trick:
function do_upload($path)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$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('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
} Then each call in loop (for loop or while: whatever) supply different path!
Hello evstevemd,
Done as per above reply
It gives error
Fatal error: Call to undefined function do_upload() in C:\xampp\htdocs\project1\system\application\controllers\admin\front.php on line 122
Thanks
do_upload($path)<--------------------did you forget $path??
No tried like this
for($i=0;$i<3;$i++)
{
if($i==0)
{
$path = ADMIN_DATA.'uploaded/shirt/parts/'.$fabric_id.'/front/thumb_img';
do_upload($path);
}
if($i==1)
{
$path = ADMIN_DATA.'uploaded/shirt/parts/'.$fabric_id.'/front/large_img';
do_upload($path);
}
if($i==2)
{
$path = ADMIN_DATA.'uploaded/shirt/parts/'.$fabric_id.'/front/join_img';
do_upload($path);
}
}
function do_upload($path)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$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('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
} did you do it all in controller class in the first code only modifying the do upload?
Yes I have done in controller class only .
Yes I have done in controller class only .
can you post current code?
<?php
class Front extends Controller
{
function Front()
{
parent::Controller();
if($this->session->userdata('adminid')=='')
{
redirect(base_url().'index.php/admin/login');
}
$this->load->model('login_model');
}
function index()
{
$this->load->view('admin/index');
}
function manage_front()
{
$data['front']=$this->login_model->getAllfront();
$this->load->view('admin/manage_front',$data);
}
function add_front($id)
{
$data['frontdata']=NULL;
$data['fab_id']=$id;
$this->load->view('admin/add_front',$data);
}
function do_upload($path)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$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('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
function action($stat,$id)
{
if($stat==1)
{
$string = mt_rand();
extract($_POST);
$fabric_id=$id;
$front_short_images=$_FILES['front_short_images']['name'];
$front_middle_images=$_FILES['front_middle_images']['name'];
$front_join_image=$_FILES['front_join_image']['name'];
for($i=0;$i<3;$i++)
{ if($i==0)
{
$path = ADMIN_DATA.'uploaded/shirt/parts/'.$fabric_id.'/front/thumb_img';
do_upload($path);
}
if($i==1)
{
$path = ADMIN_DATA.'uploaded/shirt/parts/'.$fabric_id.'/front/large_img';
do_upload($path);
}
if($i==2)
{ $path = ADMIN_DATA.'uploaded/shirt/parts/'.$fabric_id.'/front/join_img';
do_upload($path);
}
}
$data['frontdata']=$this->login_model->create_frontstyle($fabric_id);
$this->session->set_flashdata('flash', 'Front Style added successfully.');
redirect('admin/front/manage_front');
}
} I will test later, now I have another thing to do!
Ok not an issue ,Actually I have done it another way but that is not good way .
function set_folder()
{
// print_r($this->upload->data());
$data = $this->upload->data();
$udata = $this->upload_model->insert_file($data['file_name'], $_POST['title']);
/**********create folder**************/
$location = $_SERVER['DOCUMENT_ROOT'].'/online_docket/uploaded_doc/';
$folderName = $_POST["title"];
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);
//echo $location.$folderName ;
//echo $file_name ;
if(!file_exists($folderName) && !is_dir($folderName))
{
mkdir($location.$folderName, 0777, TRUE);
}
}
call this class in ur do_upload class in controller.
I thought this question was just posted some 20+ hours ago, and then I just realized it was awaken from the bone files of two years ago. :).
code is not working
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.