PHP Learning Note 38 "Download"

Source: Internet
Author: User

<?PHP//Demo Download a picture   $file _name= "Sunset.jpg"; $file _name=Iconv("Utf-8", "gb2312",$file _name);//the file name is processed because PHP is not friendly with Chinese support "Turn Utf-8 into gb2312"//1. Open the file to determine if there is   if(!file_exists($file _name)){        Echo"File does not exist"; return    ; }   $fp=fopen($file _name, "R");//open a file in read-only mode  $file _size=filesize($file _name); Echo"File Size:".filesize($file _name); //input File Label    Header("Content-type:application/octet-stream"); Header("Accept-ranges:bytes"); Header("Accept-length:".)$file _size); Header("Content-disposition:attachment; Filename= ".$file _name); //loopback data to the client    $buffer= 1024;//define the size of the data to be read//In order to download security, you can do a file byte read Word converter    $file _count=0;  while(!feof($fp) && ($file _size-$file _count>0))//feof to determine if a file is closed    {      $file _data=fread($fp,$buffer);//reading Data      $file _count+=$buffer;//statistics Read how many bytes//send some data back to the browser      Echo $file _data; }    //Close File    fclose($fp);?>

PHP Learning Note 38 "Download"

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.