Php 下载前如何正确更改文件名?
我试图在下载前更改文件名,但下载后文件会损坏。如何修复Php 下载前如何正确更改文件名?,php,downloadfile,Php,Downloadfile,我试图在下载前更改文件名,但下载后文件会损坏。如何修复 $origin_filename = 'orign_filename.exe'; $destination_filename = 'changed_filename.exe'; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition
$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);
我可以建议您使用此功能(摘自)
:“文件名后面的字符串应始终放在引号中”(如果这不能解决您的问题,请给我们一个更好的问题描述,而不仅仅是从“损坏”开始。)下载前您在哪里更改文件名
force_download()
只需要一个参数,在您的代码中传递两个参数。force_download
变量中,第一个参数要求新名称,第二个参数是实际文件名/directoryWhyforce_download()
定义没有两个参数,是否已经传递了第二个参数?
$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);