簡體   English   中英

使用CodeIgniter無法上傳PDF

[英]Uploading a PDF with CodeIgniter not working

我正在嘗試使用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
    }

我還在config文件夾的mimes.php中添加了這一行:

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

編輯:

好像有東西修復了它。 我在控制器中添加了這兩行代碼,即可完成工作。

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

您需要使用以下內容:

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

當您使用表格上傳文件時,這是必需的。 它將enctype= multipart/form-data到表單聲明中。

如果以上方法無效,您也可以更改allowed_types聲明:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM