【问题标题】:Code Igniter File Upload, Unable to upload pdf and doc, result no file uploaded without an errorCode Igniter 文件上传,无法上传 pdf 和 doc,导致没有文件上传没有错误
【发布时间】:2020-12-02 21:36:48
【问题描述】:

我第一次使用上传图片,效果很好。 然后我想升级以上传 pdf、doc 和 xls。 图像和 xls 工作,upload_folder 中有文件,文件可以下载。

我尝试上传 pdf 和 docs 时的问题,文件未上传到 upload_folder 这是我的代码:

mimes.php

'pdf'   =>  array('application/pdf', 'application/force-download', 'application/x-download', 'binary/octet-stream'),

core.php 用于下载文件

function download($src)
{
    $this->load->helper('download');
    force_download('uploads/'.$src, NULL);
}

insert.php 用于上传文件

function insert(){
        
        $id_car_header  = $this->input->post('input_id_car_header');
        $keterangan     = $this->input->post('input_keterangan');
        $arsip = $_FILES["input_arsip"]['name'];
       
        if ($arsip ='')
        {}
        else
        {   
            $config ['allowed_types']   = 'jpg|jpeg|png|docx|pdf|xls';
            $arsip_generate =$_FILES["input_arsip"]['name'];
            $config ['upload_path']     = './uploads';
            $config['file_name'] = $arsip_generate;
                    $this->load->library('upload', $config);
                    $this->load->library('upload', $config);
                //$this->upload->initialize($config);
                if (!$this->upload->do_upload('input_arsip')) 
                    {
                         //$this->upload->display_errors();
                       echo "Gambar Gagal DiUpload..!!";
                    } else{
                            $arsip = $this->upload->data('file_name');
                    }
        }

        $data = array
        (
            'id_car_header' => $id_car_header,
            'ket_arsip'    =>  $keterangan,
            'datas'        =>  $arsip
        );

        $check = $this->m_db->insertDataAll("tbl_car_arsip", $data);
        $this->checkInsert($check);     
    }

我的布局形式

<form action="<?php echo base_url().'/insert/insertArsip'; ?>" method="post" enctype="multipart/form-data">

                    <div class="col-md-12 col-xs-12">
                        <div class="form-group">
                            <input type="hidden" name="input_id_car_header" value="<?= $carHeader['id_car_header']?>">
                           
                             <label class="control-label col-md-3 col-sm-3 col-xs-12">File/Foto </label>
                            <div class="col-md-9 col-sm-9 col-xs-12">
                                
                               <input type="file" name="input_arsip" class="form-control">
                            </div>
                        </div>
                       
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">Keterangan </label>
                            <div class="col-md-9 col-sm-9 col-xs-12">

                                <textarea name="input_keterangan" required="required" class="form-control" rows="3" placeholder="Masukan Keterangan"></textarea>
                            </div>
                        </div>
                        
                        <div class="ln_solid"></div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-9 col-sm-9 col-xs-12 col-md-offset-3">
                           
                                <button type="submit" class="btn btn-success pull-right">
                                    <i class="fa fa-save"></i> Save
                                </button>
                            
                        </div>
                    </div>
</form>

我试图更改我的 mime.php,并初始化和更改结构代码,但所有这些都不起作用

有人可以告诉我我需要做什么才能使我的代码能够上传 pdf 和文档吗? 非常感谢

【问题讨论】:

  • 请从标题中删除 [SOLVED] 并根据您的发现创建答案:请参阅 Can I answer my own question
  • 我已经改了,也许你有不同的方法来解决。
  • 现在,请将工作代码作为答案,并说明您为使其工作所做的更改
  • 哦,是的,我不知道这是否是解决我问题的更好方法,谢谢。现在我的代码也许可以帮助像我这样有问题的其他人

标签: php codeigniter file-upload codeigniter-3


【解决方案1】:

我不知道为什么关闭笔记本电脑并稍微更改代码后会起作用,所有大文件都可以上传和下载 ofc 我在 Stackoverflow 上更改了一些像我一样有问题的用户的代码,谢谢

我认为这个使上传的 pdf 工作正常,我改变了这一行

   $config ['allowed_types']   = '*';

这是上传文件的更新,我只更改了这个功能

$id_car_header  = $this->input->post('input_id_car_header');
        $keterangan     = $this->input->post('input_keterangan');
        $arsip = $_FILES["input_arsip"]['name'];
      
            $config ['allowed_types']   = '*';
            $arsip_generate =$_FILES["input_arsip"]['name'];
            $config ['upload_path']     = './uploads';
            $config['file_name'] = $arsip_generate;
            $config['max_size'] = 2000000;
            $config['max_width'] = 1500;
            $config['max_height'] = 1500;
            
                    $this->load->library('upload', $config);
                    $this->upload->initialize($config, true);
                    //$this->upload->initialize($config);
                    if (!$this->upload->do_upload('input_arsip')) 
                        {
                            $error = array('error' => $this->upload->display_errors()); 
                            $this->upload->display_errors();
                             //$this->upload->display_errors() 
                            echo "Gambar Gagal DiUpload..!!";
                            var_dump($config);
                    } else{
                            
                            $arsip = $this->upload->data('file_name');
                            $data = array
                                (
                                    'id_car_header' => $id_car_header,
                                    'ket_arsip'    =>  $keterangan,
                                    'datas'        =>  $arsip
                                );
                    $check = $this->m_db->insertDataAll("tbl_car_arsip", $data);
                    $this->checkInsert($check); 
 
                    }

【讨论】:

    猜你喜欢
    • 2020-09-04
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2012-07-05
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多