Home > Backend Development > PHP Problem > How to save downloaded files in php

How to save downloaded files in php

藏色散人
Release: 2023-03-11 14:00:01
Original
3386 people have browsed it

php method to save downloaded files: 1. Download files through the "function downfile(){...}" method; 2. Save and download through the header function.

How to save downloaded files in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php download and save the file and save it to the local Two implementation methods

The download here refers to the pop-up download prompt box.

The first method:

<?php 
function downfile()
{
 $filename=realpath("resume.html"); //文件名
 $date=date("Ymd-H:i:m");
 Header( "Content-type:  application/octet-stream "); 
 Header( "Accept-Ranges:  bytes "); 
Header( "Accept-Length: " .filesize($filename));
 header( "Content-Disposition:  attachment;  filename= {$date}.doc"); 
 echo file_get_contents($filename);
 readfile($filename); 
}
downfile();
?>
Copy after login

or (recommended this method, personal test is feasible, others have not been tested)

<?php 
function downfile($fileurl)
{
 ob_start(); 
 $filename=$fileurl;
 $date=date("Ymd-H:i:m");
 $size=readfile($filename);
 header( "Content-type:  application/octet-stream "); 
 header( "Accept-Ranges:  bytes "); 
 header( "Content-Disposition:  attachment;  filename= {$date}.doc"); 
 header( "Accept-Length: " .$size);
}
 $url="url地址";
 downfile($url);
?>
Copy after login

The second method:

<?php 
function downfile($fileurl)
{
$filename=$fileurl;
$file  =  fopen($filename, "rb"); 
Header( "Content-type:  application/octet-stream "); 
Header( "Accept-Ranges:  bytes "); 
Header( "Content-Disposition:  attachment;  filename= 4.doc"); 
$contents = "";
while (!feof($file)) {
 $contents .= fread($file, 8192);
}
echo $contents;
fclose($file); 
}
$url="url地址";
downfile($url);
?>
Copy after login

PHP implements two methods for downloading files. Share it so friends who may find it useful can take a look.

Method 1:

<?php
/**
* 下载文件
* header函数
*
*/
header(&#39;Content-Description: File Transfer&#39;);
 
header(&#39;Content-Type: application/octet-stream&#39;);
header(&#39;Content-Disposition: attachment; filename=&#39;.basename($filepath));
header(&#39;Content-Transfer-Encoding: binary&#39;);
header(&#39;Expires: 0′);
header(&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(&#39;Pragma: public&#39;);
header(&#39;Content-Length: &#39; . filesize($filepath));
readfile($file_path);
?>
Copy after login

Understand the usage of header function in php.

Method 2:

<?php
//文件下载
//readfile
$fileinfo = pathinfo($filename);
header(&#39;Content-type: application/x-&#39;.$fileinfo[&#39;extension&#39;]);
header(&#39;Content-Disposition: attachment; filename=&#39;.$fileinfo[&#39;basename&#39;]);
header(&#39;Content-Length: &#39;.filesize($filename));
readfile($thefile);
exit();
?>
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to save downloaded files in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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