Php 如何基于文件id使用CodeIgniter从数据库下载文件?
这是我的代码,但无论如何都不起作用 那么,如何根据文件id使用Codeigniter实现下载功能?试试以下方法:Php 如何基于文件id使用CodeIgniter从数据库下载文件?,php,codeigniter,Php,Codeigniter,这是我的代码,但无论如何都不起作用 那么,如何根据文件id使用Codeigniter实现下载功能?试试以下方法: $this->load->helper('download'); //get file info from database $id = $this->uri->segment(4); $fileInfo['gambar'] = $this->mod_surat_masuk->download($id); //download file fro
$this->load->helper('download');
//get file info from database
$id = $this->uri->segment(4);
$fileInfo['gambar'] = $this->mod_surat_masuk->download($id);
//download file from directory
$file = 'gambar_product'.$file['gambar'];
force_download($file, NULL);
CodeIgniter有一个很好的帮助功能来下载文件
<?php
$this->load->helper('download');
//get file info from database
$id = $this->uri->segment(4);
$fileInfo = $this->mod_surat_masuk->download($id);
$source_file = file_get_contents(base_url() . 'gambar_product/' . $fileInfo['NAME_OF_THE_FILE']);
$file_name = $fileInfo['NAME_OF_THE_FILE'];
ob_clean();
force_download($file_name, $source_file);
?>
在视图部分(download\u view.php
),只需调用下载函数即可
public function download ($file_path = ""){
// load ci download helder
$this->load->helper('download');
// get download file path and store it in $data array
$data['download_file'] = $file_path;
// load view file
$this->load->view("download_view",$data);
redirect(current_url(), "refresh");
}
echo$文件;死亡代码>和发布数据请回显$file并告诉我$file上显示的路径?路径是im上载文件的目录。。。。。或者,如果你们得到了一些与我的问题类似的代码,请在这里发布它,需要输出,而不是你的意见,以便帮助$fileInfo['gambar']
返回什么(比如回显实际输出并在这里发布)?另外,我非常确定force_download
需要一个完整的路径,所以像image/somefile.jpg
这样的东西必须是/var/www/html/image/somefile.jpg
(完整路径+根目录)才能工作。我尝试根据数据库上的文件id下载文件,而不是固定文件@Pathik venjaniw文件名是什么@RizkiKurniawan@PhatikVenjani Thx它工作了,但是当我试图下载并打开文件时,文件被破坏了,或者是什么,是pdf还是什么?jpg,还有pdf@Rabbid76:请不要在软件、库或语言的名称中添加代码格式。在写作术语中,它们只是专有名词,所以这里通常要纠正的是case。但是,文件名通常被格式化为通用计算机I/O,因此如果您看到这些文件名,可以对其进行反勾选。
if(! empty($download_file))
{
$data = file_get_contents(base_url("/path/".$download_file));
// Read the file's contents
$name = $download_file;
force_download($name, $data);
}