Use PHP header function to implement file download and save to Local

Source: Internet
Author: User
Tags ranges response code

The header () function sends the original HTTP header to the client.

It is important to recognize that the header () function must be invoked before any actual output is sent (in PHP Tutorial 4 and later, you can use output caching to solve this problem):

<?php
Error results
Output already exists before calling header ()
Header (' location:http://www.111cn.net/');
?> syntax
Header (string,replace,http_response_code) parameter description
String required. Specify the header string to send.
Replace is optional. Indicates whether the header replaces the previous header, or adds a second header.

The default is true (replace). False (allows multiple headers of the same type).

Http_response_code Optional. Forces the HTTP response code to the specified value. (PHP 4 and later available)

<?php
function Downfile ()
{

$filename =realpath ("resume.html");
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($filename));
Header ("content-disposition:attachment; Filename= 4.html ");
Echo file_get_contents ($filename);
ReadFile ($filename);
}
Downfile ();

?>

<?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 =$_request[' url '];
$url = "Http://www.111cn.net";
Downfile ($url);

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.