2016-11-18 104 views
0

我有几个文件保存在服务器上的文件夹和它们的路径存储在数据库中的表hwhic看起来像这样不能下载的文件保存在服务器上的文件夹中笨

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

谁能告诉我怎么可以用笨

+2

它应该是目录路径..不是** ** HTTP –

+0

@MittulAtTechnoBrave你的意思是我应该只使用$ student_file? – user3732711

+1

请查看https://www.codeigniter.com/userguide2/helpers/download_helper.html –

回答

0

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

<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> 
0

正常下载这些文件使用IP地址或域名定位文件不起作用。您应该使用某种方式通过本地服务器路径访问文件。尝试$path = $this->input->server('DOCUMENT_ROOT');代替$path = 'www.xyz.com/';

相关问题