简体   繁体   English

File_get_contents():未能打开 stream:连接被拒绝

[英]File_get_contents(): failed to open stream: Connection refused

I am trying to download a previously uploaded file, form database and uploads folder in php codeigniter.我正在尝试在 php codeigniter 中下载以前上传的文件、表单数据库和上传文件夹。 I am using code below but downloading empty file.我正在使用下面的代码,但下载的是空文件。

controller.php 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 events_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查看.php

<div class="form-group col-md-4">
              <a href="<?=base_url('admin/incidents/downloadFile/' .$incidents->incidents_id)?>">Download file </a>
 </div>

so running this code download an empty file.所以运行此代码下载一个空文件。

echo $file;回声$文件; die;死; displays the file name which been saved in db and in uploads folder显示保存在 db 和 uploads 文件夹中的文件名

echo $path;回声$路径; die;死; generates an error: Severity: Warning生成错误:严重性:警告

Message: file_get_contents(http://localhost:8080/ticketing_tool_v2/uploads/Screenshot 2021-03-04 at 5.59.38 PM.png): failed to open stream: Connection refused消息:file_get_contents(http://localhost:8080/ticketing_tool_v2/uploads/Screenshot 2021-03-04 at 5.59.38 PM.png):无法打开 stream:连接被拒绝

Filename: admin/Incidents.php文件名:admin/Incidents.php

Line Number: 380行号:380

Reviewing the documentation for file_get_contents you'll observe there's many different ways you can use it.查看file_get_contents的文档,您会发现有许多不同的方式可以使用它。 For your purposes you would need to allow inbound connections to the filesystem.出于您的目的,您需要允许到文件系统的入站连接。

The other way you could do this for better future proofing is to use the CodeIgniter file helper - https://codeigniter.com/userguide3/helpers/file_helper.html为了更好地进行未来验证,另一种方法是使用 CodeIgniter 文件帮助器 - https://codeigniter.com/userguide3/helpers/file_helper.ZFC35FDC70D5FC69D269883A82E2

Before reading the file from path, please check whether a path points to a valid file.在从路径读取文件之前,请检查路径是否指向有效文件。

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);
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:file_get_contents无法打开流:连接被拒绝 - PHP: file_get_contents failed to open stream: Connection refused PHP- file_get_contents 无法打开 stream:连接被拒绝 - PHP- file_get_contents failed to open stream: Connection refused 警告:file_get_contents 无法打开流:连接被拒绝 php - Warning: file_get_contents failed to open stream: Connection refused php 无法在file_get_contents中打开流 - Failed to open stream in file_get_contents file_get_contents()无法打开流: - file_get_contents() failed to open stream: file_get_contents(https://xxxxx/js/nav_index.php?lang = zn&index = 1):无法打开流:在common / header.php中拒绝连接 - file_get_contents(https://xxxxx/js/nav_index.php?lang=zn&index=1): failed to open stream: Connection refused in common/header.php on line file_get_contents 无法打开 stream http 请求失败 - file_get_contents failed to open stream http request failed 无法读取XML文件。 (file_get_contents无法打开流:连接超时) - Can't read XML file. (file_get_contents failed to open stream: Connection timed out) file_get_contents返回无法打开流:连接超时错误 - file_get_contents returns failed to open stream: Connection timed out error file_get_contents():无法打开流:没有这样的文件或目录 - file_get_contents(): failed to open stream: No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM