search
HomeBackend DevelopmentPHP ProblemHow to download and modify file name in php

php method to download and modify the file name: first set the download address to "/download.php?controller=down_file&file=1.zip"; then control the output name in the Controller.

How to download and modify file name in php

Recommended: "PHP Video Tutorial"

phpModify the file name when downloading the file

Download address:

/download.php?controller=down_file&file=1.zip

Then control the output name in the Controller to achieve

$file = './路径/1.zip';
filename = '2.zip';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-type:text/html;charset=utf-8");
header('Content-Disposition: attachment; filename='. $filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($file);
exit;

==============

<?php
$file_name="aa.rar";//需要下载的文件
$file_name=iconv("utf-8","gb2312","$file_name");
$fp=fopen($file_name,"r+");//下载文件必须先要将文件打开,写入内存
if(!file_exists($file_name)){//判断文件是否存在
    echo "文件不存在";         //如果不存在
    exit();                              //直接退出
}                                         //如果存在,继续执行下载
$file_size=filesize("aa.rar");//判断文件大小
//返回的文件
Header("Content-type: application/octet-stream");
//按照字节格式返回
Header("Accept-Ranges: bytes");
//返回文件大小
Header("Accept-Length: ".$file_size);
//弹出客户端对话框,对应的文件名
Header("Content-Disposition: attachment; filename=".$file_name);
//防止服务器瞬时压力增大,分段读取
$buffer=1024;
while(!feof($fp)){
    $file_data=fread($fp,$buffer);
    echo $file_data;
}
//关闭文件
fclose($fp);
?>

=========

We generally implement downloads by calling the url to download, but this method cannot be used when IE can recognize the opened file, such as downloading a Pictures, html web pages, etc., then programming is required to implement them. The following php code can solve the problem:

<?
if( empty($_GET[&#39;FileName&#39;])|| empty($_GET[&#39;FileDir&#39;])|| empty($_GET[&#39;FileId&#39;])){
    echo&#39;<script> alert("非法连接 !"); location.replace ("index.php") </script>&#39;; exit();
}
$file_name=$_GET[&#39;FileName&#39;];
$file_dir=$_GET[&#39;FileDir&#39;];
$FileId=$_GET[&#39;FileId&#39;];
$file_dir = $file_dir."/";
if   (!file_exists($file_dir.$file_name))   {   //检查文件是否存在  
  echo   "文件找不到";  
  exit;    
  }   else   {  
$file = fopen($file_dir . $file_name,"r"); // 打开文件
// 输入文件标签
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($file_dir . $file_name));
Header("Content-Disposition: attachment; filename=" . $file_name);
// 输出文件内容
echo fread($file,filesize($file_dir . $file_name));
fclose($file);
exit();
}
?>

The above is the detailed content of How to download and modify file name in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.