首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在下载前正确更改文件名?

如何在下载前正确更改文件名?
EN

Stack Overflow用户
提问于 2021-03-01 23:19:59
回答 1查看 28关注 0票数 0

我尝试在下载前更改文件名,但文件在下载后损坏。如何修复?

代码语言:javascript
复制
  $origin_filename = 'orign_filename.exe';
  $destination_filename = 'changed_filename.exe';
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename=' . $destination_filename);
  header('Content-Transfer-Encoding: binary');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . filesize($origin_filename));
  
  readfile($origin_filename);
EN

回答 1

Stack Overflow用户

发布于 2021-03-02 00:06:43

我建议你使用这个函数(取自)

代码语言:javascript
复制
$origin_filename = 'orign_filename.exe';
$destination_filename = 'changed_filename.exe';

function force_download($file){
  header('Pragma: public');
  header('Expires: 0');
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Cache-Control: private', false);
  header('Content-Type: application/force-download');
  header('Content-length: '.filesize($file));
  header('Content-Disposition: attachment; filename="'.basename($file).'"');
  header('Content-Transfer-Encoding: binary');
  header('Connection: close');
  readfile($file);
  exit;
}

force_download($destination_filename,$origin_filename);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66424787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档