cakephp - 如何避免在下载或打开我的文件时附加 .html 扩展名

标签 cakephp

当我将文件保存在 img/upload 文件夹中时,文件会以正确的文件扩展名保存。

但是,当我尝试下载该文件时,会附加一个 .htm 文件扩展名。

我怎样才能避免这种情况?我在下面添加了我的代码;

View .ctp

<?php echo $this->Form->label("Resume:");?> 
      <?php echo $this->Form->input("resume",array("class"=>"input_boxstyle_select","label"=>"","type"=>"file","id"=>"file"));?> 
      <a href="../download_resume/<?php echo $editEmpPros[0]['prospective_employee']['resume']?>" style="margin-left:140px;color:#0477CA;"> <?php echo $editEmpPros[0]['prospective_employee']['resume']?> </a> 

在我的 Controller 中:

public function download_resume($id=null)
{
    $LUser = $this->Session->read('username');  
    $this->disableCache(); 
    if (!$LUser) {
        $this->redirect(array("action"=>"../"));                 
    }

    $path="../webroot/img/upload/$id";
    header('Content-Disposition: attachment'); readfile($path);
    //print_r(readfile($path));
    exit;
}

最佳答案

在 CakePHP 2.x 中处理文件下载

虽然其他解决方案可能有效,但 CakePHP 通过 CakeResponse object 处理响应.手册中的这一章描述了如何发送(下载)文件; Sending Files

响应将根据文件扩展名自动尝试设置正确的 mime 类型

输出文件(在浏览器内);

$this->response->file(WEBROOT_DIR . '/img/upload/' . $filename);

//Return reponse object to prevent controller from trying to render a view
return $this->response;

下载文件(并且可以选择指定自定义文件名)

要强制下载 文件并指定自定义文件名(如果需要),请使用此代码。 CakeResponse 对象将自动设置正确的 header ,因此不需要手动指定自定义文件名

// To force *downloading* the file and specify a custom filename (if desired)
$this->response->file(
    WEBROOT_DIR . '/img/upload/' . $filename,
    array(
        'download' => true,
        'name'     => 'custom-filename-for-downloading'
    )
);
return $this->response;

关于cakephp - 如何避免在下载或打开我的文件时附加 .html 扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16354508/

相关文章:

php - CakePHP 使用 Auth 记住我

mysql - cakephp - 更大的 (>) 不工作

.htaccess - 如何禁用单个文件夹的 CakePHP 重写路由,以便它可以用作第二个应用程序的位置?

php - CakePHP:添加 DISTINCT 以查找导致关联被省略

jquery - 单击添加选项时如何附加类似的表单?

php - CakePHP 基本身份验证和 jQuery

apache - 由于可能的配置错误,请求超出了 10 个内部重定向的限制

php - 将数据从 Controller 发送到 View 还是在 View 中有逻辑?

javascript - 使用 CakePHP 的文本输入创建弹出窗口

php - 如何获取由cakephp中的updateAll()函数更新的行的id?