【发布时间】:2021-03-15 15:00:02
【问题描述】:
我正在尝试在 php codeigniter 中下载以前上传的文件、表单数据库和上传文件夹。我正在使用下面的代码,但下载的是空文件。
controller.php
public function downloadFile($incidents_id)
{
$file = $this->incidents_model->getfile($incidents_id);
//echo $file; die;
$this->load->helper('download');
$path = file_get_contents(base_url()."uploads/".$file); //(the error shows on this line)
// echo $path; die;
$name = $file; // new name for your file
// echo $name ; die;
force_download($name, $path); // start download`
}
incidents_model.php
function getfile($Incident_id )
{
$file = $this->db->select('file');
$this->db->from('incidents');
$this->db->where('incidents_id' , $Incident_id );
$query = $this->db->get();
// return $query->row();
if ($query->num_rows() > 0) {
return $query->row()->file;
}
return false;
}
view.php
<div class="form-group col-md-4">
<a href="<?=base_url('admin/incidents/downloadFile/' .$incidents->incidents_id)?>">Download file </a>
</div>
所以运行此代码下载一个空文件。
回显 $file;死;显示保存在 db 和 uploads 文件夹中的文件名
回显 $path;死;产生错误: 严重性:警告
消息: file_get_contents(http://localhost:8080/ticketing_tool_v2/uploads/Screenshot 2021-03-04 at 5.59.38 PM.png):无法打开流:连接 拒绝
文件名:admin/Incidents.php
行号:380
【问题讨论】:
-
您尝试过什么调试问题?为什么不直接从磁盘读取文件呢?
-
从磁盘读取是什么意思?
-
http://localhost:8080/ticketing_tool_v2/uploads/Screenshot 2021-03-04 at 5.59.38 PM.png通过 HTTP 从另一个 URL 触发下载。如果您的文件位于同一个文件系统上,请直接打开它们 -
我试过了,还是一样的错误
标签: php codeigniter download