Skip to content

Instantly share code, notes, and snippets.

@rintoug
Created September 3, 2019 08:05
Show Gist options
  • Save rintoug/a770a02da30ed0dde9e6560367f77f12 to your computer and use it in GitHub Desktop.
Save rintoug/a770a02da30ed0dde9e6560367f77f12 to your computer and use it in GitHub Desktop.
Upload form for upload image
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Upload_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url', 'form');
}
public function index()
{
$this->load->view('upload_form');
}
public function upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048000; //2MB
$config['max_width'] = 1500;
$config['max_height'] = 1500;
$this->load->library('upload', $config);
if($this->upload->do_upload('image')) {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>Image Upload Results</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div>
<div class="container">
<div class="py-5 text-center">
<h2>Upload Form</h2>
</div>
<div class="row">
<div class="col-md-12 mb-6 text-center">
<form method="post" style="max-width: 400px;margin: auto;" enctype="multipart/form-data" action="<?php print site_url('upload_controller/upload')?>">
<?php
if (isset($error)){
echo '<div class="alert alert-danger">'.$error.'</div>';
}
?>
<div class="form-group">
<label for="firstName">Select Image</label>
<input type="file" class="form-control" name="image" id="image" placeholder="" value="" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
</div>
</div>
</div>
<!--/.container-->
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload Results</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div>
<div class="container">
<div class="py-5 text-center">
<h2>Upload Form</h2>
</div>
<div class="row">
<div class="col-md-12 mb-6 text-center">
<form style="max-width: 400px;margin: auto;" enctype="multipart/form-data" action="<?php print site_url('upload_controller/upload')?>">
<div class="alert alert-success">Uploaded Successfully!</div>
<div>File <b><?php print $upload_data['file_name']?></b> uploaded to <b><?php print $upload_data['file_path']?></b></div>
</form>
</div>
</div>
</div>
<!--/.container-->
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment