Advertisement
Guest User

Untitled

a guest
Jun 8th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.03 KB | None | 0 0
  1. <?php
  2. class ReadFile{
  3.  
  4.     var $targetFile;
  5.     var $error;
  6.     var $errorText;
  7.     var $data;
  8.     var $timeout;
  9.  
  10.     function ReadFile($targetFile){
  11.         //INITIALIZE
  12.         $this->targetFile=$targetFile;
  13.         $this->error=false;
  14.         $this->timeout=10; //TIMEOUT IN SECONDS
  15.          
  16.         if(!empty($this->targetFile)){
  17.             $this->downloadContents();
  18.         }
  19.         else{
  20.             $this->errorText="Filename not specififed in class constuctor";
  21.             $this->error=true;
  22.         }
  23.     }
  24.      
  25.     function getFileContents(){
  26.         if(!$this->hasError()){
  27.             return $this->data;
  28.         }
  29.         return false;
  30.     }
  31.      
  32.     function hasError(){
  33.         if($this->error){
  34.             return true;
  35.         }
  36.         return false;
  37.     }
  38.      
  39.     function getError(){
  40.         return $this->errorText;
  41.     }
  42.  
  43.     function downloadContents(){
  44.         $data=NULL;
  45.         //CHECK TO SEE IF WE ARE USING PHP 4.3+
  46.         if(function_exists('file_get_contents')){
  47.             $this->data=$this->openWithFileGetContents();
  48.         }
  49.         else{
  50.             $this->data=$this->openWithFOpen();
  51.         }
  52.     }
  53.      
  54.     function openWithFileGetContents(){
  55.         $data=NULL;
  56.         //SET TIMEOUT (PHP 4.3+ ONLY)
  57.         ini_set('default_socket_timeout', $this->timeout);
  58.         //RETRIEVE FILE
  59.         if(!$data=@file_get_contents($this->targetFile)){
  60.             $this->errorText='file_get_contents of ' . $this->targetFile . ' failed.';
  61.             $this->error=true;
  62.         }
  63.         return $data;
  64.     }
  65.      
  66.     function openWithFOpen(){
  67.         $data=NULL;
  68.         //RETRIEVE FILE
  69.         if($dataFile = @fopen($this->targetFile, "r" )){
  70.             while (!feof($dataFile)) {
  71.                 $data.= fgets($dataFile, 4096);
  72.             }
  73.             fclose($dataFile);
  74.         }
  75.         else{
  76.             $this->errorText='fopen of ' . $this->targetFile . ' failed.';
  77.             $this->error=true;
  78.         }
  79.         return $data;
  80.     }
  81. }
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement