Codeigniter 2.0:使用不同名称的表单输入上载各种文件

时间:2015-03-05 23:37:19

标签: php codeigniter file-upload upload codeigniter-2

我花了几个星期处理这个问题,不得不向你寻求帮助,我正在使用Codeigniter 2.0,并从autoload.php加载file_upload库,但由于某种原因文件不能上传,并且在我执行do_upload功能时没有显示任何错误,这里有一些我有很多代码(我有9个文件输入,在这个例子中只显示一个,为了您的舒适)

查看

<?php
    $atributos = array(
        "id" => "form_anuncio",
        "name" => "form_anuncio"
    );
    echo form_open_multipart("usuarios/GrabarAnuncio",$atributos);
?>
<div class="form-group">

    <label for="logotipo">Logotipo
        <input type="file" name="logotipo" id="logotipo" placeholder="Logotipo" />
    </label>

    <?php if (($datos_usuario["Logotipo"] != NULL) ||  ($datos_usuario["Logotipo"] != "")) { ?>
        <img src="<?php echo base_url(); ?>images/logos/<?php echo $datos_usuario["idusuario"] . "_" . $datos_usuario["Logotipo"] ?>" alt="Logotipo Usuario" class="img-responsive" />
    <?php } ?>
</div>

<div class="form-group">
    <input type="submit" name="GuardarAnuncio" id="GuardarAnuncio" value="Guardar Anuncio" />
</div>

<?php echo form_close(); ?>

控制器

public function GrabarAnuncio()
{
    $this->load->model('Usuarios_model');
    $this->Usuarios_model->Grabar_anuncio();
    $this->miperfil();
}

模型(仅提取)

function Grabar_anuncio()
{
     /* Lot of code  */
        $directorio = './images/';
        $directorio1 = './images/logos/' . $ID . '/';
        if (!is_dir($directorio1)) {
            mkdir($directorio1, 0777, true);
        }
        $directorio2 = './images/fotos/' . $ID . '/';
        if (!is_dir($directorio2)) {
            mkdir($directorio2, 0777, true);
        }
        $directorio3 = './images/galerias/' . $ID . '/';
        if (!is_dir($directorio3)) {
            mkdir($directorio3, 0777, true);
        }

        if ($_FILES['logotipo']['name'] != "")
        {
            $config['upload_path'] = $directorio1;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '200';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $config['overwrite'] = true;
            $config['file_name'] = $_FILES['logotipo']['name'];

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('logotipo'))
            {
                //Never Enter Here
                $serverError = true;
                print_r($this->upload->display_errors());
                exit();
            }
        }
     /* More code  */
}

如果您需要更多详细信息,只需询问,我希望您能帮助我

更新:

这是我在上传之前进行var_dump时得到的结果

array (size=9)
  'logotipo' => 
    array (size=5)
      'name' => string 'grinch1.png' (length=11)
      'type' => string 'image/png' (length=9)
      'tmp_name' => string 'G:\wamp\tmp\php5E16.tmp' (length=23)
      'error' => int 0
      'size' => int 58066
  'FotosLocalizacion1' => 
    array (size=5)
      'name' => string 'anviz1.jpg' (length=10)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'G:\wamp\tmp\php5E46.tmp' (length=23)
      'error' => int 0
      'size' => int 219877
  'FotosLocalizacion2' => 
    array (size=5)
      'name' => string 'anviz2.jpg' (length=10)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'G:\wamp\tmp\php5E47.tmp' (length=23)
      'error' => int 0
      'size' => int 172127
  'FotosLocalizacion3' => 
    array (size=5)
      'name' => string 'anviz3.jpg' (length=10)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'G:\wamp\tmp\php5E57.tmp' (length=23)
      'error' => int 0
      'size' => int 136494
  'FotosLocalizacion4' => 
    array (size=5)
      'name' => string '' (length=0)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => int 0
  'galeria1' => 
    array (size=5)
      'name' => string '' (length=0)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => int 0
  'galeria2' => 
    array (size=5)
      'name' => string '' (length=0)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => int 0
  'galeria3' => 
    array (size=5)
      'name' => string '' (length=0)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => int 0
  'galeria4' => 
    array (size=5)
      'name' => string '' (length=0)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => int 0

更新2

在if语句之后(括号外)执行此操作:die(var_dump($ this-&gt; upload-&gt; data()));

array (size=14)
  'file_name' => string 'grinch1.png' (length=11)
  'file_type' => string 'image/png' (length=9)
  'file_path' => string 'G:/wamp/www/serviciosycomprasx/images/logos/127/' (length=48)
  'full_path' => string 'G:/wamp/www/serviciosycomprasx/images/logos/127/grinch1.png' (length=59)
  'raw_name' => string 'grinch1' (length=7)
  'orig_name' => string 'grinch1.png' (length=11)
  'client_name' => string 'grinch1.png' (length=11)
  'file_ext' => string '.png' (length=4)
  'file_size' => float 56.71
  'is_image' => boolean true
  'image_width' => int 280
  'image_height' => int 280
  'image_type' => string 'png' (length=3)
  'image_size_str' => string 'width="280" height="280"' (length=24)

1 个答案:

答案 0 :(得分:1)

更改此

function Grabar_anuncio()
{
     /* Lot of code  */
        $directorio = './images/';
        $directorio1 = './images/logos/' . $ID . '/';
        if (!is_dir($directorio1)) {
            mkdir($directorio1, 0777, true);
        }
        $directorio2 = './images/fotos/' . $ID . '/';
        if (!is_dir($directorio2)) {
            mkdir($directorio2, 0777, true);
        }
        $directorio3 = './images/galerias/' . $ID . '/';
        if (!is_dir($directorio3)) {
            mkdir($directorio3, 0777, true);
        }

        if ($_FILES['logotipo']['name'] != "")
        {
            $config['upload_path'] = $directorio1;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '200';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $config['overwrite'] = true;
            $config['file_name'] = $_FILES['logotipo']['name'];

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('logotipo'))
            {
                //Never Enter Here
                $serverError = true;
                print_r($this->upload->display_errors());
                exit();
            }
        }
     /* More code  */
}

对于此

function Grabar_anuncio()
{
     /* Lot of code  */
        $directorio = './images/';
        $directorio1 = './images/logos/' . $ID . '/';
        if (!is_dir($directorio1)) {
            mkdir($directorio1, 0777, true);
        }
        $directorio2 = './images/fotos/' . $ID . '/';
        if (!is_dir($directorio2)) {
            mkdir($directorio2, 0777, true);
        }
        $directorio3 = './images/galerias/' . $ID . '/';
        if (!is_dir($directorio3)) {
            mkdir($directorio3, 0777, true);
        }

        if ($_FILES['logotipo']['name'] != "")
        {
            $config['upload_path'] = $directorio1;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '200';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $config['overwrite'] = true;
            $config['file_name'] = $_FILES['logotipo']['name'];

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('logotipo'))
            {
                //Never Enter Here
                $serverError = true;
                print_r($this->upload->display_errors());
                exit();
            } else { 
                $this->upload->data();    
            }
        }
     /* More code  */
}