简体   繁体   English

在 CodeIgniter 中强制下载

[英]Force download in CodeIgniter

public function download($id,$file_name)
{
    $this->load->helper('download');
    force_download( NULL,base_url().'uploads/files/'.$file_name);
}

I'm using this function to download a file.我正在使用此功能下载文件。 The id and file name of the file is passed into the function.文件的 id 和文件名被传递到函数中。 But whenever it download that file, it's size becomes very less and also the file does'nt execute.但是每当它下载该文件时,它的大小就会变得非常小,并且该文件也不会执行。

Things that I've already tried:我已经尝试过的事情:

  • Tried to replace NULL with some random name, the same thing happened试图用一些随机名称替换 NULL,同样的事情发生了
  • Replace the file name and file path, the same thing happened替换文件名和文件路径,同样的事情发生了
  • Tried to pass those values by storing in some variables, same happened试图通过存储在一些变量中来传递这些值,同样发生了

Things I would like to mention:我想提的事情:

  • The files are stored in localhost I'm using wamp server文件存储在我使用 wamp 服务器的本地主机中
  • Not original files are being downloaded, means it automatically creates a new file of the same name or the name that I've passed in the function.没有下载原始文件,这意味着它会自动创建一个同名或我在函数中传递的名称的新文件。

  • The files are either .exe or .apk which I want to download这些文件是我要下载的 .exe 或 .apk

What I want to do is that, I want to download same original file or even it is a copy of original then it has to be executable (for .exe)我想要做的是,我想下载相同的原始文件,甚至它是原始文件的副本,然后它必须是可执行的(对于 .exe)

Just do your modification.做你的修改。

function download($path, $name)
{
  // make sure it's a file before doing anything!
  if(is_file($path))
  {
    // get the file mime type using the file extension
    $this->load->helper('file');

    $mime = get_mime_by_extension($path);

    // Build the headers to push out the file properly.
    header('Pragma: public');     // required
    header('Expires: 0');         // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
    header('Cache-Control: private',false);
    header('Content-Type: '.$mime);  // Add the mime type from Code igniter.
    header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($path)); // provide file size
    header('Connection: close');
    readfile($path); // push it out
    exit();
}

You are passing null to the filename parameter, you need to pass it to the data parameter (2nd parameter), so your code should be like this您将null传递给filename参数,您需要将其传递给data参数(第二个参数),因此您的代码应该是这样的

public function download($id,$file_name)
{
    $this->load->helper('download');
    force_download(FCPATH.'/uploads/files/'.$file_name, null);
}

also, don't concatenate the base_url() with the path, you can pass the path to the base_url('/path/to/file') function and it'll return the link to that file.另外,不要将base_url()与路径连接,您可以将路径传递给base_url('/path/to/file')函数,它会返回指向该文件的链接。

I notice that you are passing the $id variable but it's not used, are you using it in another part of the code that is not included?我注意到您正在传递$id变量但没有使用它,您是否在未包含的代码的另一部分使用它?

And why do you even have a function to just call the force_download() function?为什么你甚至有一个函数来调用force_download()函数? can't you just call it inside the Controller?你不能在控制器内部调用它吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM