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