CodeIgniter中PDF上传本地文件夹及邮件发送问题求助
CodeIgniter PDF上传及邮件附件问题
我尝试了所有可能的方法,仍无法在CodeIgniter中上传PDF文件。上传该文件的最终目的是将其作为附件随邮件发送,目前邮件能接收其他数据,但无法获取PDF附件。我希望将文件保存至upload文件夹,或者不保存文件直接将其作为邮件附件发送,恳请提供帮助。
控制器代码
public function resume_send(){ $img = $this->input->post('resume'); $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf'; $config['max_size'] = '1000'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $config['encrypt_name'] = TRUE; $this->load->library('upload', $config); // $this->upload->do_upload('resume'); exit; if($_POST || $_FILES){ if(!$this->upload->do_upload('resume')){ echo $this->upload->display_errors('<p>', '</p>'); } else { $w = $this->upload->data(); $data = array( 'resume' => $w['file_name'], ); } } }
视图页面代码
<div class="col-6 col-md-6"> <i>Upload Resume</i> <input style="margin-bottom: 0 !important;padding: 0 !important;margin-top: 15px !important;" type="file" name="resume" required> </div>
文件夹结构

问题分析与解决方法
核心问题点
- 表单缺少
enctype="multipart/form-data"属性,这是文件上传的必要条件,没有它服务器无法识别上传的文件。 - 上传配置中
max_width和max_height是针对图片的验证项,PDF文件没有宽高属性,会导致上传失败。 - 上传判断逻辑不严谨,
$_POST || $_FILES无法准确判断是否有有效文件上传。 - 未实现上传后邮件添加附件的逻辑。
解决步骤
1. 修复表单属性
在视图的<form>标签中添加enctype="multipart/form-data",示例:
<form method="post" action="/控制器名/resume_send" enctype="multipart/form-data"> <div class="col-6 col-md-6"> <i>Upload Resume</i> <input style="margin-bottom: 0 !important;padding: 0 !important;margin-top: 15px !important;" type="file" name="resume" required> <button type="submit">提交</button> </div> </form>
2. 修正上传配置
移除针对图片的宽高验证,调整控制器代码:
public function resume_send(){ $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf'; $config['max_size'] = '1000'; // 单位KB,若PDF超出1MB需调大此值 $config['encrypt_name'] = TRUE; $this->load->library('upload', $config); // 检查是否有有效文件上传 if(isset($_FILES['resume']) && $_FILES['resume']['error'] != UPLOAD_ERR_NO_FILE){ if(!$this->upload->do_upload('resume')){ echo $this->upload->display_errors('<p>', '</p>'); } else { $upload_data = $this->upload->data(); // 调用邮件发送方法 $this->send_email_with_attachment($upload_data); } } else { echo '请选择要上传的文件'; } }
3. 实现带附件的邮件发送
添加邮件发送方法,使用CodeIgniter的Email类:
private function send_email_with_attachment($upload_data){ $this->load->library('email'); // 根据你的邮件服务器配置修改以下参数 $email_config = [ 'protocol' => 'smtp', 'smtp_host' => 'smtp.example.com', 'smtp_port' => 587, 'smtp_user' => 'your_email@example.com', 'smtp_pass' => 'your_email_password', 'mailtype' => 'html', 'charset' => 'utf-8' ]; $this->email->initialize($email_config); $this->email->from('your_email@example.com', '你的名称'); $this->email->to('收件人邮箱@example.com'); $this->email->subject('用户简历附件'); $this->email->message('这是用户上传的简历附件,请查收。'); // 添加上传后的文件作为附件 $this->email->attach($upload_data['full_path']); if($this->email->send()){ echo '邮件发送成功'; // 若无需保留文件,可在此删除:unlink($upload_data['full_path']); } else { echo $this->email->print_debugger(); } }
4. 确保uploads文件夹权限
将./uploads/文件夹权限设置为755或777(根据服务器环境调整),确保服务器可写入文件。
不保存文件直接发送附件的方案
若不想将文件保存到服务器,可直接使用临时文件发送:
public function resume_send(){ if(isset($_FILES['resume']) && $_FILES['resume']['error'] == UPLOAD_ERR_OK){ $temp_file = $_FILES['resume']['tmp_name']; $file_name = $_FILES['resume']['name']; $this->send_email_with_temp_file($temp_file, $file_name); } else { echo '请选择有效的PDF文件'; } } private function send_email_with_temp_file($temp_file, $file_name){ $this->load->library('email'); // 邮件配置同上 $email_config = [ 'protocol' => 'smtp', 'smtp_host' => 'smtp.example.com', 'smtp_port' => 587, 'smtp_user' => 'your_email@example.com', 'smtp_pass' => 'your_email_password', 'mailtype' => 'html', 'charset' => 'utf-8' ]; $this->email->initialize($email_config); $this->email->from('your_email@example.com', '你的名称'); $this->email->to('收件人邮箱@example.com'); $this->email->subject('用户简历附件'); $this->email->message('这是用户上传的简历附件,请查收。'); // 使用临时文件作为附件,第三个参数为邮件中显示的文件名 $this->email->attach($temp_file, '', $file_name); if($this->email->send()){ echo '邮件发送成功'; } else { echo $this->email->print_debugger(); } }
内容的提问来源于stack exchange,提问作者nino cphilip
相关产品推荐
相关产品推荐

