Php file download class code, php file download processing class
/*
- * Function: file download
- */
- /**
- * Usage: $ download = new Download ();
- * // Set parameters
- * $ Download-> set_file_dir ("c :/");
- * $ Download-> set_file_name ("boot. ini ");
- * $ Download-> set_read_bytes (1024 );
- * // File download processing
- * $ Download-> deal_with ();
- * // Determine whether a file exists
- * If ($ download-> is_file_exist ()){
- * Echo "file_exist ";
- *} Else {
- * Echo "file_not_exist ";
- *}
- */
- Class Download {
- Private $ file = null; // file Handle
- Private $ file_dir = ""; // Directory of the file
- Private $ file_name = ""; // file name
- Private $ file_exist = false; // indicates whether the file exists. the default value is false.
- Private $ read_bytes = 0; // Number of bytes read from the file
- Private $ mode = "r"; // file access type
Public function _ construct (){
- }
Public function _ destruct (){
- If (null! = $ This-> file ){
- Fclose ($ this-> file );
- }
- }
/**
- * File download operations
- */Bbs.it-home.org
- Public function deal_with (){
- // Full file path
- $ File_path = $ this-> file_dir. $ this-> file_name;
// Check whether the file exists
- If (file_exists ($ file_path )){
- $ This-> file_exist = true;
// Open the file
- $ This-> file = fopen ($ file_path, $ this-> mode );
// Input file tag
- Header ("Content-type: application/octet-stream ");
- Header ("Accept-Ranges: bytes ");
- Header ("Accept-Length:". filesize ($ file_path ));
- Header ("Content-Disposition: attachment; filename =". $ this-> file_name );
// Output file content
- While (! Feof ($ this-> file ))
- {
- $ Out = fread ($ this-> file, $ this-> read_bytes );
- If (! Get_magic_quotes_gpc ())
- {
- Echo $ out;
- }
- Else
- {
- Echo stripslashes ($ out );
- }
- }
- // Echo fread ($ file, filesize ($ file_dir. $ file_name ));
- }
- }
/**
- * If the returned value is true or false, use the value to determine whether the object exists.
- */
- Public function is_file_exist (){
- Return $ this-> file_exist;
- }
/**
- * The parameter type is string.
- */
- Public function set_file_dir ($ file_dir = ""){
- $ This-> file_dir = $ file_dir;
- }
/**
- * The parameter type is string.
- */
- Public function set_file_name ($ file_name = ""){
- $ This-> file_name = $ file_name;
- }
/**
- * The parameter type is integer.
- */
- Public function set_read_bytes ($ read_bytes = 1024 ){
- $ This-> read_bytes = $ read_bytes;
- }
- }
- ?>
|