Php/Codeigniter文件下载


Php / Codeigniter File Download

我正在尝试下载php中的文件。我使用了codeigniter下载助手和纯php代码读取文件。我可以在浏览器的网络中获取文件的内容,但我无法下载。

当我在一个独立的php文件中尝试readfile函数时,它起了作用。

这是我下载文件的代码块。

// codeigniter
$this->load->helper('download');
$data = file_get_contents(base_url("/images/logo.png"));
force_download($decodedFileInfo->fileName, $data);
// php readfile
$file = "./images/logo.png"; //. $decodedFileInfo->fileName;
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}else{
    var_dump("hst");die;
}

谢谢。。

尝试将文件名和图像名保存在变量中,并在屏幕上进行回显。我测试了这个功能的完美工作:

$this->load->dbutil();
//Calling a store procedure 
$sp ="exec secondProc '5 Aug 2013'";
$query = $sqlsrvr->query($sp);//->result();
$jawad =  $query->result_array();
//pass it to db utility function
$new_report = $this->dbutil->csv_from_result($query);
//Downloading the file
$this->load->helper('download');
force_download('csv_file.csv', $new_report);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header('Content-Description: File Transfer');
        header("Content-type: text/csv");
        header("Content-Disposition: attachment; filename={$name}");
        header("Expires: 0");
        header("Pragma: public");