Home > Backend Development > PHP Tutorial > php file download rename

php file download rename

不言
Release: 2023-03-24 14:52:01
Original
3006 people have browsed it

The content of this article is about downloading and renaming php files. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

Download and rename php files

Download local file

$file_url = “./本地路径”
$out_filename = ‘下载后自动保存的名字’; 
if(!file_exists($file_url)) {
    echo "不存在";
}else{
       header('Accept-Ranges: bytes');
       header('Accept-Length: ' . filesize( $file_url ));
       header('Content-Transfer-Encoding: binary');
       header('Content-type: application/octet-stream');
       header('Content-Disposition: attachment; filename=' . $out_filename);
       header('Content-Type: application/octet-stream; name=' . $out_filename);
       if(is_file($file_url) && is_readable($file_url)){
            $file = fopen($file_url, "r");
            echo fread($file, filesize($file_url));
            fclose($file);
       }
}
Copy after login

Download remote file

$file_ur = ‘远程文件地址’; 
$out_filename='下载后自动保存的文件名';
$file = @fopen($file_url, "r");
if($file){
    $content="";
    while(!feof($file)){//测试文件指针是否到了文件结束的位置 
        $data=fread($file,1024); 
        $content.=$data;
    }
    fclose($file); 
    $filesize = strlen($content); 
    header('Accept-Ranges: bytes');
    header('Accept-Length: ' . $filesize);
    header('Content-Transfer-Encoding: binary');
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $out_filename);
    header('Content-Type: application/octet-stream; name=' . $out_filename);
        echo $content;
}else{
    echo "文件不存在";
}
Copy after login

Related recommendations:

##Error type of php file upload error


           

The above is the detailed content of php file download rename. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template