【问题标题】:Upload PDF on CodeIgniter 3.1.9在 CodeIgniter 3.1.9 上上传 PDF
【发布时间】:2019-06-05 13:05:23
【问题描述】:

它告诉我The filetype you are attempting to upload is not allowed.

$config['upload_path'] = './' . URL_FILES_ALUMNOS;
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
$config['file_name'] = uniqid();

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

我在 SO 中读到它可能会遗漏这个:

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

但并没有丢失。

我不知道是否需要另一部分代码来弄清楚发生了什么

谢谢

【问题讨论】:

标签: php codeigniter pdf upload codeigniter-3


【解决方案1】:

你应该试试这个代码。我希望它对你有用

表单数据是

<div class="form-group">
     <label for="pdf1">File input</label>
     <input type="file" id="pdf1" name="pdf1">
</div>

AJAX 代码

 $(document).on('submit','#form_id',function(event){
            event.preventDefault();

            var pdf1= $('#pdf1').val().split('.').pop().toLowerCase();
            if(jQuery.inArray(pdf1, ['gif', 'png', 'jpg', 'jpeg','pdf']) == -1) 
            {
                alert("invalid File extention");
                $('#pdf1') . val('');
                return false;
            }

                $.ajax({
                    url: "<?php echo base_url();?>Home/upload_pdf",
                    method: 'POST',
                    data: new FormData(this),
                    contentType: false,
                    processData: false,
                    success: function(data)
                    {

                    }
                });
        });

控制器功能

function upload_pdf(){
 $id = $this->input->post('img_id');
    $content = array(

                    'text1' => $this->input->post('text1'),
                    'text2' => $this->input->post('text2'),
                    'img2' => $this->upload_pdf_function()
                );

    // insert this array here;
}
public function upload_pdf_function()
{
    if(isset($_FILES['img2']))
    {
        $pdf1= explode('.', $_FILES['pdf1']['name']);
        $new_name = rand().'.'.$pdf1[1];
        //$destination = '/vendor_images'.$new_name;
        move_uploaded_file($_FILES['pdf1']['tmp_name'], 'directory_name/folder_name/'.$new_name);
        return $new_name;
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-25
    • 2011-08-04
    • 2020-02-05
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2015-11-16
    相关资源
    最近更新 更多