使用CodeIgniter上传PDF不起作用

时间:2014-04-22 11:47:54

标签: php codeigniter pdf file-upload

我正在尝试使用CodeIgniter框架将pdf上传到我的网站。上传JPG或PNG工作正常,但PDF不能。

我的页面上有2个表单,但如果我从控制器打印出'userfile'帖子,我会得到pdf的文件名。

这是我在我看来的代码:

    <div class="col-6 col-sm-6 col-lg-4">
        <?php
        $attributes = array('name' => 'vluchtkalenderForm', 'id' => 'vluchtkalenderForm');
        echo form_open('subgebruiker/uploadDocument', $attributes); //opening form with action at controller 'subgebruiker/uploadDocument'
        ?>
        <h3>Vluchtkalender</h3>
        <?php echo form_hidden('typeDocument', 'vluchtkalender'); ?> //hidden field to give the directory to upload to
        <input type="file" name="userfile" id="userfile">
        <span id="vluchtkalenderFout"></span>
        <br />
        <?php
        echo form_submit('vluchtkalenderForm', 'Document toevoegen');
        echo form_close();
        ?>
    </div>
    <div class="col-6 col-sm-6 col-lg-4">
        <?php
        $attributes = array('name' => 'opleervluchtenForm', 'id' => 'opleervluchtenForm');
        echo form_open('subgebruiker/uploadDocument', $attributes);
        ?>
        <h3>Opleervluchten</h3>
        <?php echo form_hidden('typeDocument', 'opleervluchten'); ?>
        <input type="file" name="userfile" id="userfile">
        <span id="opleervluchtenFout"></span>
        <br />
        <?php
        echo form_submit('opleervluchtenForm', 'Document toevoegen');
        echo form_close();
        ?>
    </div>

这是我的控制器中的代码:

    $config['upload_path'] = 'application/userdocs/' . $this->input->post('typeDocument') . '/'; // 'typeDocument' contains part of the directory where to upload to
    $config['allowed_types'] = 'pdf';
    $config['max_size'] = '0';
    $this->load->library('upload', $config);

    if ($this->upload->do_upload('userfile')) {
        $data["succesmelding"] = "Uploaden van document gelukt."; //upload worked
    } else {
        $data["foutmelding"] = "Er is een fout opgetreden bij het uploaden van het document."; //upload didn't work
    }

我还在配置文件夹中的mimes.php中添加了这一行:

'pdf'   =>  array('application/pdf', 'application/x-download', 'application/unknown'),

编辑:

似乎有些事情已经解决了。我在我的控制器中添加了这两行代码,并完成了这项工作。

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

1 个答案:

答案 0 :(得分:0)

您需要使用以下内容:

echo form_open_multipart('subgebruiker/uploadDocument', $attributes); 

当您使用表单上传文件时,这是必需的。它将enctype= multipart/form-data添加到表单声明。

如果以上操作不起作用,您也可以更改allowed_types声明:

$config['allowed_types'] = '*'; //the * for any mime type