CodeIgniter File Upload.


Project Structure


view:
htmlfile.php

<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<h3>File UPload</h3>
<?php echo form_open_multipart('myfile/file_upload');?>
<form method="post" action="<?php echo base_url(); ?>myfile/file_upload" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>

-----------------------
view:
upload_success.php

<!DOCTYPE html>
<html>
<head>
<title>Image Details Data</title>
</head>
<body>
<?php 
foreach ($upload_data as $key => $value) {
print "<pre>";
print_r($value);
}
?>
</body>

</html>
-----------------------

controller:
MyFile.php 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MyFile extends CI_Controller {
public function index() {
$this->load->view('htmlfile');
}
function file_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|xlsx|pdf';
$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);
}
}

}
-----------------------
Demo 1

Demo 2

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment