Skip to main content

How to upload file in codeigniter?

Upload file in Codeigniter?


Paste this code in controller

if($this->input->post('upload_file'))

{

$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);

// $upload_data = $this->upload->data('file_name'); 

                if ( ! $this->upload->do_upload('files'))

                {

                        $error = array('error' => $this->upload->display_errors());


                        //$this->load->view('upload', $error);

echo 'FIle Error';

                }

                else

                {

                         $data = array('upload_data' => $this->upload->data());


                       // $this->load->view('upload', $data);

echo 'FIle Uploaded Successfully';

$upload_name = $this->upload->data('file_name');

                } 

}

Paste this code in View

<div class="user_update_form">

<form method="post" action="" enctype="multipart/form-data" />

<?php echo validation_errors(); ?>  

<?php //echo $error; ?>  

<?php echo form_open_multipart('upload/do_upload');?>

<h1 class="register_lable">Upload Files</h1>

<label>Files</label>

<input type="file" class="input_field" name="files" value="">

<input type="submit" name="upload_file" class="upload_file" value="Update">


</form>

</div>

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...