[英]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.