Php file download code. could you please tell me what went wrong?-php Tutorial

Source: Internet
Author: User
Php file download code. could you please tell me what went wrong? // File download function
// $ File_path file download path
// $ File_name file name
// $ File_sub_dir download file sub-path
Function down_file ($ file_name, $ file_sub_dir ){
// If the file name is Chinese, transcode
$ File_name = iconv ("UTF-8", "gb-2312", $ file_name );
// Absolute path
$ File_path = $ _ SERVER ["DOCUMENT_ROOT"]. $ file_sub_dir. $ file_name;
// Open the file
If (! File_exists ($ file_path )){
Echo "the file does not exist! ";
Return;
}
$ Fp = fopen ($ file_path, "r ");

// Get the file size
$ File_size = filesize ($ file_path );

// Returned file
Header ("Content-type: application/octet-stream ");
// Returns the value in bytes.
Header ("Accept-Ranges: bytes ");
// Returns the file size.
Header ("Accept-length: $ file_size ");
// The client pop-up dialog box with the corresponding file name
Header ("Content-Disposition: attachment; file_name =". $ file_name );

// Send data back to the client

$ Buffer = 1024;
// To ensure secure download, make a file byte reader
$ File_count = 0;
// Determine whether the object is ended
While (! Feof ($ fp) & ($ file_size-$ file_count> 0 )){
$ File_data = fread ($ fp, $ buffer );
// How many bytes are read
$ File_count + = $ buffer;
// Pass part of the data to the browser
Echo $ file_data;

}
// Close the file
Fclose ($ fp );
}
File_down ("1.jpg","/test /");
?>


Reply to discussion (solution)

Below is an error

Warning: fopen (C:/AppServ/www/test/) [function. fopen]: failed to open stream: Permission denied in C: \ AppServ \ www \ test \ FileDownServer. php on line 16

Warning: Cannot modify header information-headers already sent by (output started at C: \ AppServ \ www \ test \ FileDownServer. php: 16) in C: \ AppServ \ www \ test \ FileDownServer. php on line 22

Warning: Cannot modify header information-headers already sent by (output started at C: \ AppServ \ www \ test \ FileDownServer. php: 16) in C: \ AppServ \ www \ test \ FileDownServer. php on line 24

Warning: Cannot modify header information-headers already sent by (output started at C: \ AppServ \ www \ test \ FileDownServer. php: 16) in C: \ AppServ \ www \ test \ FileDownServer. php on line 26

Warning: Cannot modify header information-headers already sent by (output started at C: \ AppServ \ www \ test \ FileDownServer. php: 16) in C: \ AppServ \ www \ test \ FileDownServer. php on line 28

Warning: feof (): supplied argument is not a valid stream resource in C: \ AppServ \ www \ test \ FileDownServer. php on line 36

Warning: fread (): supplied argument is not a valid stream resource in C: \ AppServ \ www \ test \ FileDownServer. php on line 37

Warning: feof (): supplied argument is not a valid stream resource in C: \ AppServ \ www \ test \ FileDownServer. php on line 36

Warning: fread (): supplied argument is not a valid stream resource in C: \ AppServ \ www \ test \ FileDownServer. php on line 37

Warning: feof (): supplied argument is not a valid stream resource in C: \ AppServ \ www \ test \ FileDownServer. php on line 36

Warning: fopen (C:/AppServ/www/test/) [function. fopen]: failed to open stream: Permission denied in C: \ AppServ \ www \ test \ FileDownServer. php on line 16
It's very clear!
Permission denied

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.