It is used to upload external content in an application.
The file is a collection of records that is used to store data under computer harddisk.
CI provides Upload Library to upload data.
Step1st:-
Create a Controller to upload a file and create a view and load view under the controller method.
Step2nd:-
Create Folder under project under application
Step3rd:-
Create action method do_upload() and write file uploading script
Step4th:-
Run application to change config.php
Code of Controller
<?php
class FileUpload extends CI_Controller
{
function index()
{
$this->load->view('fileview');
}
function do_upload()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$data['res']=$this->upload->display_errors();
}
else
{
$data['res']="uploading sucessfully";
}
$this->load->view('fileview',$data);
}
}
?>
Code of View:-
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('fileupload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
<?php
echo @$res;
?>
</body>
</html>
How to upload file under database:-
Create Database and table:-
Create Table imgid int ,imgpath text ,imgdesc text
Create Model
<?php
class Uploadmodel extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function upload($path,$idesc)
{
$res=$this->db->insert('tbl_img',array('imgpath'=>$path,"imgdesc"=>$idesc));
return $this->db->affected_rows();
}
function showfile()
{
return $this->db->get('tbl_img')->result_array();
}
}
?>
and create View file
<?php
for($i=0;$i<count($res);$i++) {?>
<img src="<?php echo base_url().'upload/'.$res[$i]['imgpath'] ?>" width="100" height="100"/>
<br>
<p><?php echo $res[$i]['imgid']; ?></p>
<?php
}
?>
The file is a collection of records that is used to store data under computer harddisk.
CI provides Upload Library to upload data.
Step1st:-
Create a Controller to upload a file and create a view and load view under the controller method.
Step2nd:-
Create Folder under project under application
Step3rd:-
Create action method do_upload() and write file uploading script
Step4th:-
Run application to change config.php
Code of Controller
<?php
class FileUpload extends CI_Controller
{
function index()
{
$this->load->view('fileview');
}
function do_upload()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$data['res']=$this->upload->display_errors();
}
else
{
$data['res']="uploading sucessfully";
}
$this->load->view('fileview',$data);
}
}
?>
Code of View:-
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('fileupload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
<?php
echo @$res;
?>
</body>
</html>
How to upload file under database:-
Create Database and table:-
Create Table imgid int ,imgpath text ,imgdesc text
Create Model
<?php
class Uploadmodel extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function upload($path,$idesc)
{
$res=$this->db->insert('tbl_img',array('imgpath'=>$path,"imgdesc"=>$idesc));
return $this->db->affected_rows();
}
function showfile()
{
return $this->db->get('tbl_img')->result_array();
}
}
?>
and create View file
<?php
for($i=0;$i<count($res);$i++) {?>
<img src="<?php echo base_url().'upload/'.$res[$i]['imgpath'] ?>" width="100" height="100"/>
<br>
<p><?php echo $res[$i]['imgid']; ?></p>
<?php
}
?>
POST Answer of Questions and ASK to Doubt