gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:41:15 29 4
gpt4 key购买 nike

当我将文件保存在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com