无法下载保存在服务器文件夹中的文件


Not able to download files kept on server folder in codeigniter

我有几个文件保存在服务器文件夹中,它们的路径存储在数据库表中,看起来像这样

 Table Name: student_file
    id  name     file
    1   abc     asd/fgh/abc.docx
    2   def     asd/fgh/def.pdf
    3   ghi     asd/fgh/ghi.doc

我已经从上面的表(student_file)中获取了数据,并以表格形式显示,其中"下载"将是一个链接或按钮,看起来像这样

abc   download
def   download
ghi   download

现在我想如果用户点击第一次下载,那么abc的文件应该被下载,同样的第二和第三。

我使用的代码正在下载文件,但当我试图在我的系统上打开下载的文件时,它说文件没有正确下载。

视图

<a href="<?php echo base_url()?>admin/download/1">Download</a>
控制器

public function download($id)
    {
        $data['download'] = $this->admin_model->get_download_data($id);
    }

模型
public function get_download_data($id)
    {
        $this->db->where('id',$id);
        $data = $this->db->get('student_file');
        $student_data = $data->row();
        $student_file = $student_data->file;
        $ext = substr($student_file, strpos($student_file, ".") + 1);
        $path = 'www.xyz.com/';
        $file = $path . $student_file;
        header("Content-type:application.$ext");
        header("Content-Disposition:attachment;filename='downloaded.$ext");
        readfile($file);
    }

谁能告诉我如何使用codeigniter正确下载这些文件

我认为你可以很容易地做到这一点,

<a href="<?php echo base_url(); ?>path/to/file/abc.docx" target="_blank" ></a>
<a href="<?php echo base_url(); ?>path/to/file/def.pdf" target="_blank" ></a>
<a href="<?php echo base_url(); ?>path/to/file/ghi.docx" target="_blank" ></a>

使用ip地址或域名瞄准文件不起作用。您应该使用某种方式访问具有本地服务器路径的文件。用$path = $this->input->server('DOCUMENT_ROOT');代替$path = 'www.xyz.com/';