How to create file uploader in codeigniter? - codeigniter

I see CI documentation to create an image uploader.
here is my Controller :
<?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()
{
$this->load->helper(array('form', 'url'));
$this->load->helper('url');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
echo $this->upload->display_error();
return FALSE;
}
else
{
$data = $this->upload->data();
return TRUE;
}
}
}
and here is my View
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<form action="upload/do_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
But when i upload an image it doesn't upload it and $this->upload->display_error() show nothing(my uploads folder which is in my ci root folder also has 777 permision and i'm using wamp)

Two pointers for you:
<input type="file" name="userfile" size="20" />
Check if the file you're using is not exceeding and the file is being uploaded.
echo $this->upload->display_errors();
See the display_errors with an s.
Suggestions:
In you form's action attribute use a an absolute URL, like:
action="<?php echo base_url() ?>upload/do_upload"
Inside your function do_upload(), do a print_r($_FILEs), to confirm that the file is uploaded correctly with no errors.

I change my function named function do_upload() to function somethingelse() and my problem solved (because i'm calling a built in function)

Related

Undefined property: Main::$upload , Codeigniter

I am trying to upload single image using codeigniter. But getting error
"Undefined property: Main::$upload
Filename: controllers/Main.php
Line Number: 14"
This is my controller file "controller/Main.php"
<?php
class Main extends CI_Controller
{
public function index(){
$this->load->view('main_view', array('error' => ''));
}
public function upload(){
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|png|gif';
$this->load->initialize('upload', $config);
if(!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('main_view', $error);
}
else {
$file_data = $this->upload->data();
$data['img'] = base_url().'/images/'.$file_data['file_name'];
$this->load->view('success_msg', $data);
}
}
}
?>
and this is my view file "views/main_view.php"
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<?php echo $error; ?>
<?php echo form_open_multipart('main/upload');?>
<input type="file" name="userfile" />
<input type="submit" name="Submit" value="upload image"/>
</form>
</body>
</html>
when image upload is successful I'm trying to show the image by this "view/success_msg.php"
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<h1>File has been uploaded.</h1>
<img src="<?php $img?>" width="300" height="300">
</body>
</html>
my image folder name is "images" and it is in the root directory. Please help my why I am having this error ?
Maybe the upload library isn't loaded. Change the first few lines ofpublic function upload() to the following
public function upload(){
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|png|gif';
$this->load->library('upload', $config);
By sending $config to load->library you do not need to use initialize().
Controller
public function upload(){
$cc = $_FILES['userfile']['name'];
$extension = pathinfo($cc, PATHINFO_EXTENSION);
$newfile = preg_replace('/[^A-Za-z0-9]/', "", $_FILES["userfile"]['name']);
$config['file_name'] = time() . $newfile;
$ss = ".";
$picture1 = time() . $newfile . $ss . $extension;
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
echo $this->upload->display_errors();
} else {
$this->load->view('success_msg', $picture1 );
}
}
View page
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<?php echo $error; ?>
<?php echo form_open_multipart('main/upload');?>
<input type="file" name="userfile" />
<input type="submit" name="Submit" value="upload image"/>
</form>
</body>
</html>
view/success_msg.php
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<h1>File has been uploaded.</h1>
<img src="<?php $picture1 ?>" width="300" height="300">
</body>
</html>

How to upload php or html file in codeigniter?

How to upload php or html file in codeigniter using upload library? I have controller which working ok when uploading jpg, gif, png files but it won't to upload files with php or html or sql extension.
Here is my controller
public function ipload()
{
$this->load->helper('url');
$this->load->model('m_company');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'php|html|txt';
$config['max_size'] = 2048;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$data = array('error' => $this->upload->display_errors());
}
else
{
$this->upload->do_upload('userfile');
$data = array('upload_data' => $this->upload->data());
$filen = $this->upload->data('file_name');
$this->m_company->set_doc($filen);
}
echo json_encode($data);
}
When allowed files are jpg gif png everything works fine
first you setting controler, you can use access to upload file all class using $config['allowed_types'] ='*';
class c_cutipegawai extends CI_Controller{
function __construct(){
parent::__construct();
}
function do_upload(){
$config['upload_path'] ='./upload/SuratCuti/';
$config['allowed_types'] ='*';
$config['max_size'] =1024*8;
$config['max_widht'] =1024*2;
$config['max_height'] =768;
$this->load->library('upload', $config);
if(! $this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
}
else{
$data=array('upload_data' => $this->upload->data());
}
}
}
After setting in controler, now we craete view to insert button upload file
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('C_CutiPegawai/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>

how to upload multiple files in one <input>

I have made a code that let me upload multiple files but in separate
I was trying to upload a multiple files in one input where i set my input tag into
<input type="file" multiple="" name="file1">
I selected 3 images but only 1 image was uploaded..
here is my VIEW before changing my input:
<?php echo form_open_multipart('test'); ?>
<p>
<?php echo form_label('File 1: ', 'file1') ?>
<input type='file' name='file1' id='file1'>
</p>
<p>
<?php //echo form_label('File 2: ', 'file2') ?>
<input type='file' name='file2' id='file2'>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
and here is my CONTROLLER
function index()
{
if (isset($_POST['submit']))
{
$this->load->library('upload');
$config['upload_path'] = './upload_documents/';
$config['allowed_types'] = 'jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
foreach($_FILES as $field => $file)
{
// No problems with the file
if($file['error'] == 0)
{
// So lets upload
if ($this->upload->do_upload($field))
{
$data = $this->upload->data();
//alert("nice");
}
else
{
$errors = $this->upload->display_errors();
die();
}
}
else{
echo "alksjdfl";
die();
}
}
}
$this->load->view("test");
}
}
Use this multi file upload library
https://github.com/stvnthomas/CodeIgniter-Multi-Upload/blob/master/MY_Upload.php
// Prepraing upload config & upload files
$config = array();
$config['upload_path'] = 'temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '256000';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_multi_upload("my_file");
$files_upload = $this->upload->get_multi_upload_data();
$err_msg = $this->upload->display_errors();
if (!empty($files_upload) && is_array($files_upload)) {
foreach ($files_upload as $file_data) {
// Your files here :)
}
}
Special thanks to #stvnthomas for the library :)
You should use this library for multi upload in CI
https://github.com/stvnthomas/CodeIgniter-Multi-Upload
Installation
Simply copy the MY_Upload.php file to your applications library directory.
Use: function test_up in controller
public function test_up(){
if($this->input->post('submit')){
$path = './public/test_upload/';
$this->load->library('upload');
$this->upload->initialize(array(
"upload_path"=>$path,
"allowed_types"=>"*"
));
if($this->upload->do_multi_upload("myfile")){
echo '<pre>';
print_r($this->upload->get_multi_upload_data());
echo '</pre>';
}
}else{
$this->load->view('test/upload_view');
}
}
upload_view.php in applications/view/test folder
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" id="myfile" multiple>
<input type="submit" name="submit" id="submit" value="submit"/>

Ajax file upload with Codeigniter and jQuery Form Plugin

I'm tying to use jQuery Form Plugin with Codeigniter. When I try to upload, the page is redirected (so the ajax is not working) and nothing is uploaded.
The view from where the form is located look like this:
<form id="upload_form" action="upload/do_upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"><br>
<input type="submit" value="Upload File to Server">
</form>
<div class="progress">
<div class="bar"></div >
<div class="percent">0%</div >
</div>
<div id="status"></div>
<script>
(function() {
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
$('#upload_form').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
complete: function(xhr) {
status.html(xhr.responseText);
}
});
})();
</script>
<script type="text/javascript" src="http://localhost/editor/assets/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://localhost/editor/assets/js/jquery.form.js"></script>
</body>
And the controller for the upload is just taken from the Codeigniter manual:
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function do_upload()
{
$config['upload_path'] = './userdata/';
$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); TODO echo error
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data); TODO echo succes
}
}
}
?>
I'm sure I've just made a little mistake somewhere, could any one see where I went wrong? Thanks in advance!
It looks like you forgot to initialize the form when the DOM is ready.
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#upload_form').ajaxForm(function() {
//rest of your code from the post
});
});

How can i insert data to mysql database with image name which will be auto upload image without submit and instant display

My code is below
View
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="<?php echo base_url();?>index.php/setup_con/add_user_info">
Picture <input name="userfile" type="file" />
Name <input name="name" type="text" />
<input name="" type="submit" />
</form>
Controller
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '800';
$config['max_width'] = '184';
$config['max_height'] = '142;
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo $error;
}
else
{
$data = array('upload_data' => $this->upload->data());
$img = $data ['upload_data']['file_name'];
$data = $this->setup_model->add_user_info($img);
}
Model
$name = $this->input->post('name');
$img = $img
$data = array (
'img' =>$img,
'name' =>$name
);
$this->db->insert('user',$data);
Normally i can do it
Basically ,i want to do at first auto image upload without submit(may be via ajax) and
instantly display then i will submit then insert data to user table with image name and name.
Please help me how can i solve this problem
Sorry if I have misread your question, but this sounds like a really long way of asking how to preview an image before upload. Here is a way of doing that before you do any upload, you can then do your actual image upload along with your database stuff all together on submit.
HTML
<img id="preview_img" src="" />
<input id='img_upload' type="file" name="userfile" />
JQUERY
$('body').on("change", "#img_upload", function(event) {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#preview_img').attr('src', e.target.result);
}
reader.readAsDataURL(this.files[0]);
}
});

Resources