Implementation code for PHP (HTTP protocol) file download

Source: Internet
Author: User
Tags fread ranges
A PHP code to implement file download using the HTTP protocol, for in-depth understanding of the principle of PHP file download, is a good example of the need for friends, really should refer to the study.

1. File download code

     0) {$file _data=fread ($fp, $buffer);  Count the number of bytes read $file _count+= $buffer; Return part of the data to the browser echo $file _data; }//Close file fclose ($fp);? >

2. Encapsulation function:

     {echo "file is too large to download";  return; }*///http Download the desired response header header ("Content-type:application/octet-stream");   Returns the header of the file ("Accept-ranges:bytes"); Returns the header by byte size ("Accept-length: $file _size"); Returns the file size header ("content-disposition:attachment;  Filename= ". $file _name);//The Client's popup dialog box, corresponding filename//return data to the client//set size output $buffer = 1024;  In order to download security, we'd better make a file byte read counter $file _count=0; Determines whether the file pointer is to the end of the file (read whether the file ends) while (!feof ($fp) && ($file _size-$file _count) >0) {$file _data=fread ($fp, $   Buffer);   Count the number of bytes read $file _count+= $buffer;  Return part of the data to the browser echo $file _data; }//Close file fclose ($FP); } file_dowm ("Bjnihao.jpg", "/http/dowm/");? >
  • 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.