[英]Uploading a PDF with CodeIgniter not working
I'm trying to upload a pdf to my website using the CodeIgniter framework. 我正在尝试使用CodeIgniter框架将pdf上传到我的网站。 Uploading a JPG or PNG works fine, but a PDF doesn't.
上载JPG或PNG效果很好,但PDF则不行。
I have 2 forms on my page, but if I print out the 'userfile' post from my controller, I do get the filename of the pdf. 我的页面上有2种表单,但是如果我从控制器打印出“ userfile”帖子,则可以获取pdf的文件名。
This is the code I have in my view: 这是我认为的代码:
<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>
This is the code in my controller: 这是我的控制器中的代码:
$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
}
I also added this line in mimes.php in the config folder: 我还在config文件夹的mimes.php中添加了这一行:
'pdf' => array('application/pdf', 'application/x-download', 'application/unknown'),
EDIT: 编辑:
Seems like something fixed it. 好像有东西修复了它。 I added these two lines of code in my controller and that did the job.
我在控制器中添加了这两行代码,即可完成工作。
$this->upload->initialize($config);
$this->load->library('upload', $config);
You need to use the following: 您需要使用以下内容:
echo form_open_multipart('subgebruiker/uploadDocument', $attributes);
This is required as you are uploading a file with the form. 当您使用表格上传文件时,这是必需的。 It adds the
enctype= multipart/form-data
to the form declaration. 它将
enctype= multipart/form-data
到表单声明中。
You may also change the allowed_types
declaration if the above doesn't works: 如果以上方法无效,您也可以更改
allowed_types
声明:
$config['allowed_types'] = '*'; //the * for any mime type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.