Codeigniter 2.0:上传具有不同名称的表单输入的各种文件


Codeigniter 2.0: Upload various files with form inputs with different names

我花了几个星期的时间处理这个问题,不得不向您寻求帮助,我正在使用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->upload->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)

更改此

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  */
}