CI 3.0控制器:客户上传文件后台下载异常问题求助
问题
客户首次登录仪表板会弹出表单,填写后可上传PDF和图片。管理员后台查看条目时,能下载第二个文件(支持JPG/PNG/PDF等),但无法下载第一个文件(格式同样支持jpg/png/pdf)。
相关CodeIgniter 3.0控制器代码
<?php defined('BASEPATH') OR exit('No direct script access allowed'); include_once 'Admin_controller.php'; class Client extends Admin_Controller { public function __construct(){ parent::__construct(); $this->check_login(); $this->load->model('client_model'); } public function index(){ $data = $this->data; $list=array(); $data['list']=$list; $data['page_title'] = "Online Account List"; $this->page->showpage($data); } public function ajax_get_data_list(){ $where = array(); $item_list = $this->client_model->get_all($where, array('id'=>'desc')); if(empty($item_list)) $item_list = array(); $record_list = array(); if(count($item_list)>0){ foreach ($item_list as $key => $row){ $form_data = $row['form_data']; if(!empty($form_data)){ $form_data = json_decode($form_data, true); }else{ continue; } $record=array(); $record[]=$form_data['form_title']; $record[]=$form_data['first_name']; $record[]=$form_data['surname']; $record[]=$form_data['country']; $record[]=$form_data['preferred_email']; $record[]=$form_data['preferred_phone']; $record[]=$row['add_timestamp']; $html='<div class="text-right">'; $html.='<a href="javascript:void(0);" data-href="'.base_url('admin/client/detail').'" data-id="'.$row['id'].'" class="ajax-view-client-btn icon-btn btn btn-success btn-xs tooltips margin-right-5" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete"><i class="fa fa-eye fa fa-white"></i></a>'; $html.='<a href="javascript:void(0);" data-href="'.base_url('admin/client/delete').'" data-id="'.$row['id'].'" class="ajax-del-client-btn icon-btn btn btn-danger btn-xs tooltips margin-right-5" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete"><i class="fa fa-times fa fa-white"></i></a>'; $html.='</div>'; $record[]=$html; $record_list[]=$record; } } $table_data=array('data'=>$record_list); echo json_encode($table_data); } public function detail(){ $id = intval($_POST['id']); $condition['id'] = $id; $row = $this->client_model->get($condition); $form_data = $row['form_data']; if(!empty($form_data)){ $form_data = json_decode($form_data, true); }else{ $form_data = array(); $this->json_output_error("Invalid account"); return; } $row['form_data'] = $form_data; $online_form_data = $form_data; $message = ''; $message .= '<table align="center" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 100%; max-width: 960px;" class="content">'; $message .='<tbody>'; $field_data_list = get_field_list(); foreach($field_data_list as $section => $field_list) { if($section === 'photographic_id'){ continue; } if($section == 'profile_information') { $section_title = "Profile Information"; }elseif($section == 'account_information') { $section_title = "Account Information"; }else{ $section_title = "Additional Information"; } $message.='<tr><td bgcolor="#ffffff" style="padding: 20px 20px 5px 20px; color: #555555; font-family: Arial, sans-serif; font-size: 16px; font-weight: bold; line-height: 24px;">'.$section_title.'</td></tr>'; foreach($field_list as $row){ $field_name = $row['name']; $field_label = $row['label']; $field_value = get_data_value($online_form_data, $field_name); if(is_array($field_value)){ $field_value = implode(',', $field_value); } $message.='<tr><td bgcolor="#ffffff" style="padding: 3px 20px 3px 20px; color: #555555; font-family: Arial, sans-serif; font-size: 15px; line-height: 24px;">'.$field_label.' : <span style="font-weight: bold">'.$field_value.'</span></td></tr>'; } } $filename_arr = []; $section_title = "Photographic ID"; $message.='<tr><td bgcolor="#ffffff" style="padding: 20px 20px 5px 20px; color: #555555; font-family: Arial, sans-serif; font-size: 16px; font-weight: bold; line-height: 24px;">'.$section_title.'</td></tr>'; foreach($field_data_list['photographic_id'] as $row){ $field_name = $row['name']; $field_label = $row['label']; $field_value = get_data_value($online_form_data, $field_name); $file_url = base_url().UPLOADS_PATH.'/'.PROFILE_IMAGE_PATH.'/'.$field_value; $img = '<a href="'.($file_url).'" target="_blank" style="color: #007bff; text-decoration: underline" download>Download</a>'; $message.='<tr><td bgcolor="#ffffff" style="padding: 3px 20px 3px 20px; color: #555555; font-family: Arial, sans-serif; font-size: 15px; line-height: 24px;">'.$field_label.' : <span style="font-weight: bold">'.$img.'</span></td></tr>'; $filename_arr[] = UPLOADS_PATH.'/'.PROFILE_IMAGE_PATH.'/'.$field_value; } $message .='</tbody>'; $message .= '</table>'; $html = $message; $data = array( 'client_data' => $row, 'html' => $html ); $this->json_output_data($data); } public function delete(){ $id = intval($_POST['id']); $condition['id'] = $id; $data = $this->client_model->delete($condition); $this->json_output_data($data); } } ?>
截图

解决方案
排查与修复步骤
验证第一个文件的字段值有效性
在detail方法的文件循环中加入调试代码,确认第一个文件的文件名是否正确存储:foreach($field_data_list['photographic_id'] as $row){ $field_name = $row['name']; $field_label = $row['label']; $field_value = get_data_value($online_form_data, $field_name); // 打印日志检查字段值 error_log("First file field: {$field_name}, value: {$field_value}"); // ... 原有代码 }如果
$field_value为空,需要检查前端表单提交逻辑,确保第一个文件的文件名正确写入form_data。检查文件路径拼接是否正确
确认UPLOADS_PATH和PROFILE_IMAGE_PATH常量的定义,直接打印拼接后的$file_url,在浏览器中访问测试是否能打开文件:$file_url = base_url().UPLOADS_PATH.'/'.PROFILE_IMAGE_PATH.'/'.$field_value; error_log("First file URL: {$file_url}");若路径错误,修正常量定义,确保指向正确的文件存储目录。
处理空值或无效文件名
为避免空值生成无效下载链接,在生成按钮前加入判断逻辑:foreach($field_data_list['photographic_id'] as $row){ $field_name = $row['name']; $field_label = $row['label']; $field_value = get_data_value($online_form_data, $field_name); if(empty($field_value)){ $img = '<span style="color: #999;">未上传文件</span>'; } else { $file_url = base_url().UPLOADS_PATH.'/'.PROFILE_IMAGE_PATH.'/'.$field_value; $img = '<a href="'.($file_url).'" target="_blank" style="color: #007bff; text-decoration: underline" download>下载</a>'; } $message.='<tr><td bgcolor="#ffffff" style="padding: 3px 20px 3px 20px; color: #555555; font-family: Arial, sans-serif; font-size: 15px; line-height: 24px;">'.$field_label.' : <span style="font-weight: bold">'.$img.'</span></td></tr>'; if(!empty($field_value)){ $filename_arr[] = UPLOADS_PATH.'/'.PROFILE_IMAGE_PATH.'/'.$field_value; } }确认服务器文件权限
检查第一个文件的存储目录权限(通常设为755)和文件权限(通常设为644),确保Web服务器能读取该文件。
内容的提问来源于stack exchange,提问作者Vijay Singh
相关产品推荐
相关产品推荐

