• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Library Upload not working

#1
I have a form to upload an image, but in the controller when I load the library upload and use do_upload () or display_errors (), I get the following message:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Grupos::$upload
Filename: admin/grupos.php
Line Number: 66



Fatal error: Call to a member function do_upload() on null in C:\xampp\htdocs\pilloti\application\controllers\admin\grupos.php on line 66
Reply

#2
It would be better if you can post the controller, view and model code.
Reply

#3
you don't load the library
Reply

#4
(02-14-2017, 03:43 AM)andersonunsonst Wrote: you don't load the library

When I put $ this-> load-> library ('upload'); It does not give me any errors, but when I use something from the library itself.
Reply

#5
(02-14-2017, 12:21 AM)ragingTorch Wrote: It would be better if you can post the controller, view and model code.

View:
PHP Code:
<form role="form" action="<?php echo base_url(); ?>admin/grupos/guardarGrupo" name="frmNuevoGrupo" id="frmNuevoGrupo" method="post">
    <
div class="modal-header">
        <
button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <
h4 class="modal-title" id="myModalLabel">Nuevo grupo</h4>
    </
div>

    <
div class="modal-body">
        <
div class="form-group">
            <
label for="txtNombre">Nombre del grupo</label>
            <
input type="text" name="txtNombre" id="txtNombre" class="form-control">
        </
div>

        <
div class="form-group">
            <
label for="txtDescripcion">Descripción del grupo</label>
            <
textarea name="txtDescripcion" id="txtDescripcion" class="form-control" cols="10" rows="3"></textarea>
        </
div>

        <
div class="form-group">
            <
label for="txtImagen">Imagen del grupo</label>
            <
input type="file" name="txtImagen" id="txtImagen" class="form-control filestyle" data-buttonText="Buscar imagen" data-iconName="fa fa-inbox" data-buttonName="btn-danger" data-placeholder="Seleccione una imagen">
        </
div>
    </
div>

    <
div class="modal-footer">
        <
button type="submit" class="btn btn-primary">Guardar</button>
    </
div>
    </
form
Java Script:
Code:
$('#frmNuevoGrupo').submit(function(e) {
        e.preventDefault();
    }).validate({
        rules: {
            txtNombre: { required: true },
            txtDescripcion: { required: true },
            txtImagenGrupo: { required: true }
        },
        messages: {
            txtNombre: { required: 'Debe escribir el nombre del grupo.' },
            txtDescripcion: { required: 'Debe escribir la descripción del grupo.' },
            txtImagenGrupo: { required: 'Debe seleccionar una imagen para el grupo.' }
        },
        submitHandler: function(form) {
            var url = $('#frmNuevoGrupo').attr('action');
            var met = $('#frmNuevoGrupo').attr('method');
            var msg = $('#mensaje');
            var mdl = $('#mdlNuevoGrupo');

            var formData = new FormData($("#frmNuevoGrupo")[0]);
            
            $.ajax({
                url: url,
                type: met,
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: function(data) {
                    var opt = data.split('|');

                    if (opt[1] == '1') {
                        msg.removeClass('mensaje-good').addClass('mensaje-error');
                    } else {
                        msg.removeClass('mensaje-error').addClass('mensaje-good');    
                    }

                    msg.html(opt[0]);
                    msg.fadeIn('medium').delay(6000).fadeOut('medium', function() {
                        $('#tblGrupos tbody').load(base_url + 'admin/grupos/cargarGrupos');
                        $('#frmNuevoGrupo input').val('');
                        $('#frmNuevoGrupo textarea').val('');
                        mdl.modal('hide');
                    });
                }
            });
        }
    });
Controller:
PHP Code:
public function guardarGrupo()
    {
        
$config = [
 
              "upload_path"    => './images/grupos',
 
              "allowed_types"    => 'jpg'
 
          ];

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

 
          if ($this->upload->do_upload('txtImagen')) {
 
              $data = array("upload_data" => $this->upload->data());

 
              $nom $this->input->post('txtNombre');
 
              $des $this->input->post('txtDescripcion');
     
          $img $data["upload_data"]["file_name"];

     
          $res $this->Grupos_model->guardarGrupo($nom$des$img);

     
          if ($res === 'M') {
     
              $msg 'El grupo ya existe en nuestra base de datos.|1';
     
          } elseif ($res === false) {
     
              $msg 'El grupo no pudo ser guardado.|1';
     
          }
     
          else {
     
              $msg 'El grupo fue guardado con éxito.|0';
     
          }
 
          } else {
 
              $msg $this->upload->display_errors() . '|1';
 
          }

 
          echo $msg;
    } 
Model:
PHP Code:
public function guardarGrupo($nom$des$ima)
    {
        
$this->db->where('nombre'$nom);
        
$q $this->db->get('grupos');

        if (
$q->num_rows() > 0) {
            return 
'M';
        } else {
            
$data = array(
                    
'nombre'        => $nom,
                    
'descripcion'    => $des,
                    
'imagen'        => $ima
                
);

            
$q2 $this->db->insert('grupos'$data);

            if (
$q2) {
                return 
true;
            } else {
                return 
false;
            }
        }
    } 
Reply

#6
The first character of the filename should be upper case.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#7
(02-14-2017, 07:52 AM)InsiteFX Wrote: The first character of the filename should be upper case.

Where?
Reply

#8
Load the form helper in the controller before you load the form:
PHP Code:
$this->load->helper('form'); 

Then, in your view, open the form with:
PHP Code:
<?php echo form_open_multipart('admin/grupos/guardarGrupo','id="frmNuevoGrupo"');?>

Without multipart, you won't be able to handle upload files.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.