Upload an image in PHP framework CodeIgniter

0

I can not upload the image and I do not receive the name to save it in the Database , I got the following error:

  

"You did not select a file to upload"

Controller:

      $config['upload_path'] =  base_url()."assets/img/uploads/";
      $config['allowed_types'] ='jpg|jpeg|png';

    $this->load->library("upload",$config);     
    $file_data = $this->upload->data();
   $data['foto_usuario'] = base_url()."assets/img/uploads/".$file_data['file_name'];
  $form_data = array(
              'nombre' => set_value('nombre'),
              'apellido_m' => set_value('apellido_m'),
              'apellido_p' => set_value('apellido_p'),
              'password' => sha1(set_value('password')."*3c0*"),
              'username' => set_value('username'),
              'foto_usuario'=> $data['foto_usuario'],
              'created' => date('Y-m-d H:i:s'),
              'modified'=> date('Y-m-d H:i:s'),
              'role' => set_value('role')
              );

View:

<link rel="stylesheet" href="<? echo base_url();?>assets/css/form.css">

<div class="row">
<div class="col-lg-12">
    <h1 class="page-header">Agregar usuarios</h1>
</div>
<!-- /.col-lg-12 -->
</div>


 <?php // Change the css classes to suit your needs

 $attributes = array('class' => 'contact_form', 'id' => 'contact_form');
 echo form_open('usuarios/add', $attributes); ?>


  <ul>
  <span class="required_notification">* Datos requeridos</span>
  <li>
    <label for="nombre">Nombre <span class="required">*</span></label>
    <?php echo form_error('nombre'); ?>
    <input id="nombre" type="text" name="nombre" maxlength="45" value="<?php echo set_value('nombre'); ?>"  />
  </li>


   <li>
    <label for="apellido_p">Apellido Paterno <span class="required">*</span></label>
      <?php echo form_error('apellido_p'); ?>
    <br /><input id="apellido_p" type="text" name="apellido_p" maxlength="45" value="<?php echo set_value('apellido_p'); ?>"  />
  </li>
  <li>
    <label for="apellido_m">Apellido Materno</label>
    <?php echo form_error('apellido_m'); ?>
    <br /><input id="apellido_m" type="text" name="apellido_m" maxlength="45" value="<?php echo set_value('apellido_m'); ?>"  />
  </li>

  <li>
    <label for="password">Password <span class="required">*</span></label>
    <?php echo form_error('password'); ?>
    <br /><input id="password" type="password" name="password" maxlength="45" value="<?php echo set_value('password'); ?>"  />
  </li>

  <li>
    <label for="username">Email <span class="required">*</span></label>
    <?php echo form_error('username'); ?>
    <br /><input id="username" type="text" name="username" maxlength="45" value="<?php echo set_value('username'); ?>"  />
    <span class="form_hint">Formato correcto: "[email protected]"</span>
 </li>

  <li>
    <label for="role">Perfil <span class="required">*</span></label>
    <?php echo form_error('role'); ?>

    <?php // Change the values in this array to populate your dropdown as required ?>
    <?php $options = array(
                                              ''  => 'Please Select',
                                              '1'    => 'Administrador',
                                            ); ?>

    <br /><?php echo form_dropdown('role', $options, set_value('role'))?>
 </li>
 <li>
<input type="file" name="foto_usuario" value="<?php echo set_value('foto_usuario');?>"/>
 </li>

 <li>
    <?php echo form_submit( 'submit', 'Guardar',"class='btn btn-success'"); ?>
 </li>


 <?php echo form_close(); ?>
  </ul>
    
asked by Ricardo Valencia Alvarado 15.05.2017 в 17:32
source

1 answer

1

Modify the function and in this way you can upload the images without problems, as long as you create a folder Upload in the root of the file or modify the path to save where you want.

Controller function Add

function add()  {

if($this->session->userdata('is_logued_in') !=TRUE)
{
  redirect(base_url());
}
$data=$this->session->userdata();
    $this->form_validation->set_rules('nombre', 'nombre', 'required|trim|max_length[45]');
    $this->form_validation->set_rules('apellido_m', 'apellido_m', 'trim|max_length[45]');
    $this->form_validation->set_rules('apellido_p', 'apellido_p', 'required|trim|max_length[45]');
    $this->form_validation->set_rules('password', 'password', 'required|trim|max_length[45]');
    $this->form_validation->set_rules('username', 'username', 'required|trim|valid_email|max_length[45]');
    $this->form_validation->set_rules('role', 'role', 'required|trim|max_length[11]');

    $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');

    if ($this->form_validation->run() == FALSE) // validation hasn't been passed
    {
        $this->template->load('template', 'users/add.php',$data);
    }
    else // passed validation proceed to post success logic
    {
   $config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width']  = '5024';
$config['max_height']  = '5768';

$this->load->library('upload', $config);

/*verifica si existe error */
if ( ! $this->upload->do_upload())
{
  $error = array('error' => $this->upload->display_errors());
  redirect('usuarios/index');
}
else
{
  /* exito en la subida del archivo */
  $data = array('upload_data' => $this->upload->data());

  // build array for the model
  $form_data = array(
              'nombre' => set_value('nombre'),
              'apellido_m' => set_value('apellido_m'),
              'apellido_p' => set_value('apellido_p'),
              'password' => sha1(set_value('password')."*3c0*"),
              'username' => set_value('username'),
              'userfile'=> $data['upload_data']['file_name'],
              'created' => date('Y-m-d H:i:s'),
              'modified'=> date('Y-m-d H:i:s'),
              'role' => set_value('role')
              );


        // run insert model to write data to db
        if ($this->Usuario->SaveForm($form_data) == TRUE) // the information 
             has therefore been successfully saved in the db
        {
             redirect('usuarios/index');   // or whatever logic needs to 
             occur
        }
        else
        {
        echo '
         Se ha producido un error al guardar la información. Por favor, 
          inténtelo de nuevo más tarde';
        // Or whatever error handling is necessary
        }
      }
    }
  }
    
answered by 18.05.2017 в 17:36