在CodeIgniter 3.1.9上上传PDF

时间:2019-01-10 15:58:07

标签: php codeigniter pdf upload codeigniter-3

它告诉我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')

但也不失踪。

我不知道是否需要另一部分代码来了解正在发生的事情

谢谢

1 个答案:

答案 0 :(得分:0)

您应该尝试使用此代码。我希望它对您有用

<强>形式的数据是

<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;
    }
}