how to upload path in codeigniter - php

I am new to CI. Currently I have the following:
$config['upload_path'] = './uploads/';
I just want to know how to update path in codeignitor. I have tried the below code. Is there something I'm doing wrong?
<?php
defined('BASEPATH')`enter code here` OR exit('No direct script access allowed');
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);
}
}
}
?>

From Your code Your file will be uploaded at uploads folder which is in your root directory. $config['upload_path'] = './uploads/'; this is the place where your uploaded files are stored.
You make an directory uploads there where an application folder is .
If you want your current code.
You'll need a destination folder for your uploaded images. Create a folder at the root of your CodeIgniter installation called uploads and set its file permissions to 777.
By default the upload routine expects the file to come from a form field called userfile.
from file uploading class

Use this library to upload... Its easy to use
http://demo.codesamplez.com/codeigniter/file-upload-demo
View
<form action="" method="POST" enctype="multipart/form-data" >
Select File To Upload:<br />
<input type="file" name="userfile" multiple="multiple" />
<input type="submit" name="submit" value="Upload" class="btn btn-success" />
</form>
{if isset($uploaded_file)}
{foreach from=$uploaded_file key=name item=value}
{$name} : {$value}
<br />
{/foreach}
{/if}
Controller
/**
* the demo for file upload tutorial on codesamplez.com
* #return view
*/
public function file_upload_demo()
{
try
{
if($this->input->post("submit")){
$this->load->library("app/uploader");
$this->uploader->do_upload();
}
return $this->view();
}
catch(Exception $err)
{
log_message("error",$err->getMessage());
return show_error($err->getMessage());
}
}
Component
/**
* Description of uploader
*
* #author Rana
*/
class Uploader {
var $config;
public function __construct() {
$this->ci =& get_instance();
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
'upload_url' => base_url()."files/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
}
public function do_upload(){
$this->remove_dir($this->config["upload_path"], false);
$this->ci->load->library('upload', $this->config);
if($this->ci->upload->do_upload())
{
$this->ci->data['status']->message = "File Uploaded Successfully";
$this->ci->data['status']->success = TRUE;
$this->ci->data["uploaded_file"] = $this->ci->upload->data();
}
else
{
$this->ci->data['status']->message = $this->ci->upload->display_errors();
$this->ci->data['status']->success = FALSE;
}
}
function remove_dir($dir, $DeleteMe) {
if(!$dh = #opendir($dir)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!#unlink($dir.'/'.$obj)) $this->remove_dir($dir.'/'.$obj, true);
}
closedir($dh);
if ($DeleteMe){
#rmdir($dir);
}
}
}

$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('image name'))
{
$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);
}

Related

How to upload multipl file fiedls in codeigniter

I have form with 4 file fields and I want to upload these files properly.
My code is as follows :
public 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('userfile')) {
$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);
}
}
private function fileUpload($name, $config = []){
$config['upload_path'] = './uploads/';
$config['encrypt_name'] = TRUE;
$config['file_ext_tolower'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|png';
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($name))
{
$error = array('error' => true, 'message' => $this->upload->display_errors());
return $error;
}
else
{
return array('error' => false, 'upload_data' => $this->upload->data());
}
}
Create this private function in the controller just call this function when you have to upload the file with the name of the file field if you want some other config just pass config array to as a next parameter
$data = $this->fileUpload('site_logo_dark')
if($data['error']){
echo data['message']; // error message while uplaoding an file if have any
}else{
echo $data['upload_data']['file_name']; // return you a name of the file which you just upload it you can save it to the database
}

error in uploading image file

i am trying to make a user profile and add a profile picture functionality to it in codeigniter,i simpy copy pasted the file upload code given in the documentation but it is showing error.my view file is
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
my controller is
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('upload');
}
public function do_upload()
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
echo'not happening';
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
it is showing error over here i mean it is echoing 'not happening',which means it is not uploading.if someone can please help me,it will be great.
Why not you check what is the exact error? I have just updated the code to track exact error what codeigniter is returning.
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
echo '<pre>';
print_r($this->upload->display_errors());
}
You have loaded the library twice sometimes I found causes issues you have placed one in the constructor and one in the function
Try like below and use $this->upload->initialize($config); in the function and load library in the __constructor.
https://www.codeigniter.com/user_guide/libraries/file_uploading.html#preferences
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('upload');
}
public function do_upload()
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 5000;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
echo'not happening';
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
Note no need to close codeigniter controllers and models with ?> as
says in user guide
application
// Where you upload images to
images
system
index.php

Uploading multiple images with codeigniter to a database

My functions upload only one image at a time, when the form is submitted. I can not upload multiple images at once. This is a huge problem because I am building a car-sales website, and people need to upload multiple car images.
My upload.php controller:
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');
}
function index()
{
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
if($this->input->post('upload'))
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$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=$this->upload->data();
$this->thumb($data);
$file=array(
'img_name'=>$data['raw_name'],
'thumb_name'=>$data['raw_name'].'_thumb',
'ext'=>$data['file_ext'],
'upload_date'=>time()
);
$this->upload_model->add_image($file);
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
else
{
redirect(site_url('upload'));
}
}
function thumb($data)
{
$config['image_library'] = 'gd2';
$config['source_image'] =$data['full_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 160;
$config['height'] = 110;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
My upload_form.php view:
<?php $attributes = array('name' => 'myform');
echo form_open_multipart('/upload/do_upload',$attributes);?>
<input type="file" name="userfile" size="20" />
<input type="submit" value="upload" name="upload" />
<?php echo form_close(); ?>
My upload_model.php Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function add_image($data)
{
$this->db->insert('jobs',$data);
}
}
I can't modify the function in a way that allows multiple image upload. I would highly appreciate any kind of guidance or help. Thank you in advance!
#####################
# Uploading multiple#
# Images #
#####################
$files = $_FILES;
$count = count($_FILES['uploadfile']['name']);
for($i=0; $i<$count; $i++)
{
$_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
$_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
$_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
$_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
$_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());//function defination below
$this->upload->do_upload('uploadfile');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}
$fileName = $images;
what's happening in code??
well $_FILE---->it is an associative array of items uploaded to the current script via the POST method.for further look this LINK
it's an automatic variable avaliable within all scopes of script
function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
$config['remove_spaces']=TRUE;
$config['encrypt_name'] = TRUE; // for encrypting the name
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '78000';
$config['overwrite'] = FALSE;
return $config;
}
and in your html markup don't don't forget:
Input name must be be defined as an array i.e. name="file[]"
Input element must have multiple="multiple" or just multiple
3.$this->load->library('upload'); //to load library
4.The callback, $this->upload->do_upload() will upload the file selected in the given field name to the destination folder.
5.And the callback $this->upload->data() returns an array of data related to the uploaded file like the file name, path, size etc.

trying to upload two images file_path in one column in codeigniter

I am trying to upload two images file_path in one column. In my model i am getting first image path successfully from $filepath but $a is not working i am also confused that $a is getting my second image path or or not
Kindly help me
Thanks in advance.
My Controller
<?php
class upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view("main_temp/header.php");
$this->load->view('post_ad_views', array('error' => ' ' ));
$this->load->view("main_temp/footer.php");
}
// controller
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';
$config['file_name'] = $new_file_name;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view("main_temp/header.php");
$this->load->view('post_ad_views', $error);
$this->load->view("main_temp/footer.php");
}
else
{
// success
$data = array('upload_data' => $this->upload->data());
// a model that deals with your image data (you have to create this)
$this->load->model('submit_ad_model');
$this->submit_ad_model->ad_post();
$this->load->view('upload_success', $data);
}
}
}
?>
MY Model
<?php
class submit_ad_model extends CI_Model
{
function ad_post()
{
$filepath = $this->upload->data()['file_name'];
$a = $this->upload->data()['file_name'];
$this->db->query("insert into ads (ad_pic) values ('$filepath,$a')");
?>
My Views
<input type="file" name="userfile" class="upload image" id="userfile" />
<input type="file" name="userfile2" class="upload image" id="userfile" />
You arent looking to get the second uploaded item, you need to use a loop:
$upload_data = array();
foreach($_FILES as $key => $value){
$this->upload->do_upload($key);
$upload_data[$key] = $this->upload->data($key);
}
$this->submit_ad_model->ad_post($upload_data);

Check did select file Or no, in input:file

How can know, if select(Choice Or Browse...) file in input:file run some code with php?
<form action="#" method="post" enctype="multipart/form-data">
<input type="file" name="userfile[]" valign="baseline">
</form>
if(select file){
run my code
}
Update:
I use of Multiple Uploads with JQuery and Code Igniter , when that i don't select file, output print_r($_FILES['userfile']); is:
Array ( [error] => 4 [name] => [size] => 0 [tmp_name] => [type] =>
[key] => userfile )
[error] => 4 is -> You did not select a file to upload.
If i select file, have following error in case i have in input:file this: name="userfile[]":
Message: Undefined index: userfile in here:print_r($_FILES['userfile']);
Update 2:
I use from these Controller but if don't select a file, in the here if ( ! $files ) return is false and give error. i want if user did not select file does not get error with a if
class Upload extends Controller {
function Upload()
{
parent::Controller();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form');
}
function do_upload()
{
if($_FILE('userfile')){
$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);
}
}
redirect('inse/show');
}
}
<?php
if (isset($_FILES['my_file_input'])) {
// your code
}
?>
NOTE: you descripted a wrong input type if i get you right - file input should use this signature:
<input type="file" name="my_file_input" />
NOTE: the form the file input is located within should have its enctype set to "multipart-form-data" (or something like that).
UPD: i may be wrong, but here's come controller from the module you have specified:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends Controller {
function Upload()
{
parent::Controller();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form');
}
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);
}
}
}
?>
If you still do not get any satisfying results, try var_dump($_FILES); and find out how the files are stored.

Categories