【问题标题】:File_get_contents(): failed to open stream: Connection refusedFile_get_contents():无法打开流:连接被拒绝
【发布时间】: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


【解决方案1】:

查看file_get_contents 的文档,您会发现有许多不同的方式可以使用它。出于您的目的,您需要允许到文件系统的入站连接。

为了更好地进行未来验证,另一种方法是使用 CodeIgniter 文件帮助器 - https://codeigniter.com/userguide3/helpers/file_helper.html

【讨论】:

  • 您好,您能帮我如何允许到文件系统的入站连接吗?
【解决方案2】:

从路径读取文件前,请检查路径是否指向有效文件。

public function downloadFile($incidents_id) {

try {
    $this->load->helper('download');

    $file = $this->incidents_model->getfile($incidents_id);
     
    $data = file_get_contents(base_url("uploads/" . $file));

    $name = $file; // new name for your file
        
    force_download($name, $data); // start download`
     
} catch (Exception $e) {
    //exception handling code goes here
    print_r($e);
}

}

【讨论】:

  • 我已运行您的代码并收到以下错误:'遇到 PHP 错误严重性:通知消息:未定义变量:名称文件名:admin/Incidents.php 行号:404 回溯:文件:/opt /lampp/htdocs/ticketing_tool_v2/application/controllers/admin/Incidents.php 行:404 功能:_error_handler 文件:/opt/lampp/htdocs/ticketing_tool_v2/index.php 行:315 功能:require_once
  • 异常对象 ( [message:protected] => 路径localhost:8080/ticketing_tool_v2/uploads/mpdf.pdf [string:Exception:private] => [code:protected] => 0 [file:protected] 上不存在该文件=>
  • /opt/lampp/htdocs/ticketing_tool_v2/application/controllers/admin/Incidents.php [line:protected] => 404 [trace:Exception:private] => 数组 ([0] =>数组 ( [file] => /opt/lampp/htdocs/ticketing_tool_v2/system/core/CodeIgniter.php [line] => 532 [function] => downloadFile [class] => Incidents [type] => -> [args ] => 数组 ( [0] => 64 ) ) [1] => 数组 ( [file] => /opt/lampp/htdocs/ticketing_tool_v2/index.php [line] => 315 [args] => 数组 ( [0] => /opt/lampp/htdocs/ticketing_tool_v2/system/core/CodeIgniter.php ) [function] => require_once ) ) [previous:Exception:private] => )
  • 对不起,我已经更新了我的代码,请立即检查。对于异常对象( [message:protected] => 文件不存在于路径中---请检查文件是否存在于上传文件夹中或授予上传文件夹路径及其内容的正确权限
  • 所以这就是我使用您的代码得到的结果:异常对象([message:protected] => 文件 1615847175_timetable.png 不存在于路径 localhost:8080/ticketing_tool_v2/uploads/… [string:Exception:private] => [ code:protected] => 0 [file:protected] => /opt/lampp/htdocs/ticketing_tool_v2/application/controllers/admin/Incidents.php [line:protected] => 404 [trace:Exception:private] => Array ( [0] => 数组
猜你喜欢
  • 1970-01-01
  • 2013-03-27
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 2018-12-14
  • 2011-12-19
相关资源
最近更新 更多