php - Codeigniter, testing if file uploader has file -


i have following code in view:

if (isset($stockists)) {     $id = $stockists->id;     echo form_open_multipart($system_settings['admin_folder'].'/stockists/form/'.$id); }  else {     echo form_open_multipart($system_settings['admin_folder'].'/stockists/form/'); }   <?php echo "<input type='file' name='userfile' size='20' />"; ?> 

there's lots of other text input fields in there sent database on submit. file loader i'm interested in though.

in controller function, how can check if file exists in up-loader after submit?

the following retruns false: $image = ($_files['userfile']);

i need check in conditional statement if file exists in uploader. example:

if ($_files['userfile']) {   //do } 

but method not work.

the super global $_files

$_files['userfile'] isn't boolean.

if (strlen($_files['userfile']['tmp_name']) > 0) {     // yes, uploaded } 

in array, you've error:

echo $_files['userfile']['error']; 

codeigniter approach

codeigniter has upload class can work you.

codeigniter's file uploading class permits files uploaded. can set various preferences, restricting type , size of files.

below example codeigniter documentation:

<?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()     {         $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);          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);         }     } } ?> 

see full examples documentation: ci file upload class


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -