【问题标题】:Change download filename codeigniter更改下载文件名 codeigniter
【发布时间】:2017-02-14 08:03:52
【问题描述】:

在强制下载前转换文件名。

我正在尝试能够在下载之前将存储文件名的文件名转换为原始文件名

所以当用户点击文件下载而不是显示时

post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php

如下图所示

它只会将下载的文件重命名为

config.php

关于如何只在点击图片时更改下载的文件名。

public function downloads($id) {
    $this->db->where('attachment_id', $id);
    $query = $this->db->get('attachments');

    if ($query->num_rows() == 0) {
       return false;
    }

    $path = '';
    $file = '';

    foreach ($query->result_array() as $result) {

        $path .= FCPATH . 'uploads/'; 

        // This gives the stored file name
        // This is folder 201702
        // Looks like 201702/post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php

        $stored_file_name .= $result['attachment_name']; 

        // Out puts just example "config.php"
        $original .= $result['file_name']; 

    }

    force_download($path . $stored_file_name, NULL);
}

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    就这么简单!

    force_download(
        $filenameWithFileExtension, 
        file_get_contents($fileToDownload), 
        mime_content_type($fileToDownload)
    );
    

    【讨论】:

      【解决方案2】:

      怎么样 (恕我直言,你的 foreach 循环没有任何意义)

      public function downloads($id) {
          $this->db->where('attachment_id', $id);
          $query = $this->db->get('attachments');
      
          if ($query->num_rows() == 0) {
             return false;
          }
      
          $path = '';
          $file = '';
      
          foreach ($query->result_array() as $result) {
      
              $path .= FCPATH . 'uploads/'; 
      
              // This gives the stored file name
              // This is folder 201702
              // Looks like 201702/post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
      
              $stored_file_name .= $result['attachment_name']; 
      
              // Out puts just example "config.php"
              $original .= $result['file_name']; 
      
          }
      
          force_download("your_filename.txt",file_get_contents($path . $stored_file_name));
      }
      

      【讨论】:

      • 这很好,但是如果有更好的方法添加它,你的意思是 foreach 循环没有意义。
      • 想到的问题是,如果您在这里有多个结果会怎样?在这种情况下,此功能将不再起作用
      • 当我点击每个文件时,使用 $attachment = $query->row_array(); $path = FCPATH . 'uploads/'; $original = $attachment['file_name']; $stored_file_name = $attachment['attachment_name']; 之类的 row_array() 似乎可以正常工作
      【解决方案3】:

      对于这种情况,请使用类似这样的 force_download() -

      $file_name = "using md5 to convert it on normal text and stroe variable";
      $file_path = "It's your file path, where have file in your drive"
      $file_path = 'uploads/'.$path; 
      force_download($file_name, file_get_contents($file_path));
      

      它很简单而且工作正常。只需要将您的文件路径和文件名存储在 2 个不同的变量中,并将它们传递给 force_download()。 希望它的工作。 祝你好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-01
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多