php檔案操作類的代碼一例

來源:互聯網
上載者:User
某大學提供的一個php檔案操作類,帶有濃鬱的書香氣息,剛剛走出校門的朋友,不妨參考下本文的代碼,是否有似曾相識的感覺呢?程式員之家收集,供大家學習參考。

代碼如下:

fileName_str=$fileName_str;       $this->fileOpenMethod_str=$fileOpenMethod_str;   }      function __destruct()   {       //解構函式   }      public function __get($valName_val)//欲取得的資料成員名稱   {       //特殊函數,取得指定名稱資料成員的值          return $this->$valName_val;   }      private function on_error($errMsg_str='Unkown Error!',$errNo_int=0)//錯誤資訊,錯誤碼   {        echo '程式錯誤:'.$errMsg_str.'錯誤碼:'.$errNo_int;//出錯處理函數   }      public function open()   {       //開啟相應檔案,返迴文件資源標識          //根據fileOpenMethod_str選擇開啟檔案          switch($this->fileOpenMethod_str)          {                 case 'readOnly':                    $openMethod_str='r';      //唯讀,指標指向檔案頭                    break;                 case 'readWrite':                    $openMethod_str='r+';     //讀寫,指標指向檔案頭                    break;                 case 'writeAndInit':                    $openMethod_str='w';      //唯寫,指標指向檔案頭將大小截為零,不存在則建立                    break;                 case 'readWriteAndInit':                    $openMethod_str='r+';     //讀寫,指標指向檔案頭將大小截為零,不存在則建立                    break;                 case 'writeAndAdd':                    $openMethod_str='a';      //唯寫,指標指向檔案末尾,不存在則建立                    break;                 case 'readWriteAndAdd':                    $openMethod_str='a+';     //讀寫,指標指向檔案末尾,不存在則建立                    break;                 default:                    $this->on_error('Open method error!',310);//出錯處理                    exit;          }                    //開啟檔案                 if(!$fp_res=fopen($this->fileName_str,$openMethod_str))          {                 $this->on_error('Can\'t open the file!',301);//出錯處理                 exit;          }                    return $fp_res;   }      public function close($fp_res)//由open返回的資源標識   {       //關閉所開啟的檔案          if(!fclose($fp_res))          {                 $this->on_error('Can\'t close the file!',302);//出錯處理                 exit;          }   }      public function write()//$fp_res,$data_str,$length_int:檔案資源標識,寫入的字串,長度控制   {       //將字串string_str寫入檔案fp_res,可控制寫入的長度length_int          //判斷參數數量,調用相關函數          $argNum_int=func_num_args();//參數個數                    $fp_res=func_get_arg(0);          //檔案資源標識          $data_str=func_get_arg(1);        //寫入的字串                    if($argNum_int==3)          {                 $length_int=func_get_arg(2);  //長度控制              if(!fwrite($fp_res,$data_str,$length_int))              {                    $this->on_error('Can\'t write the file!',303);//出錯處理                    exit;              }          }          else          {                 if(!fwrite($fp_res,$data_str))              {                    $this->on_error('Can\'t write the file!',303);//出錯處理                    exit;              }          }   }      public function read_line()//$fp_res,$length_int:檔案資源標識,讀入長度   {       //從檔案fp_res中讀入一行字串,可控制長度          //判斷參數數量          $argNum_int=func_num_args();          $fp_res=func_get_arg(0);                    if($argNum_int==2)          {              $length_int=func_get_arg(1);              if($string_str=!fgets($fp_res,$length_int))              {                    $this->on_error('Can\'t read the file!',304);//出錯處理                    exit;              }              return $string_str;       }       else       {              if(!$string_str=fgets($fp_res))              {                    $this->on_error('Can\'t read the file!',304);//出錯處理                    exit;              }              return $string_str;          }   }      public function read($fp_res,$length_int)//檔案資源標識,長度控制   {       //讀入檔案fp_res,最長為length_int          if(!$string_str=fread($fp_res,$length_int))          {                 $this->on_error('Can\'t read the file!',305);//出錯處理                 exit;          }          return $string_str;   }      public function is_exists($fileName_str)//檔案名稱   {       //檢查檔案$fileName_str是否存在,存在則返回true,不存在返回false          return file_exists($fileName_str);   }/******************取得檔案大小*********************//*取得檔案fileName_str的大小$fileName_str 是檔案的路徑和名稱返迴文件大小的值*/   public function get_file_size($fileName_str)//檔案名稱   {       return filesize($fileName_str);   }/******************轉換檔大小的表示方法*********************//*$fileSize_int檔案的大小,單位是位元組返迴轉換後帶計量單位的檔案大小*/   public function change_size_express($fileSize_int)//檔案名稱   {       if($fileSize_int>1024)       {          $fileSizeNew_int=$fileSize_int/1024;//轉換為K          $unit_str='KB';            if($fileSizeNew_int>1024)             {              $fileSizeNew_int=$fileSizeNew_int/1024;//轉換為M              $unit_str='MB';             }          $fileSizeNew_arr=explode('.',$fileSizeNew_int);          $fileSizeNew_str=$fileSizeNew_arr[0].'.'.substr($fileSizeNew_arr[1],0,2).$unit_str;       }       return $fileSizeNew_str;   }/******************重新命名檔案*********************//*將oldname_str指定的檔案重新命名為newname_str$oldName_str是檔案的原名稱$newName_str是檔案的新名稱返回錯誤資訊*/    public function rename_file($oldName_str,$newName_str)   {          if(!rename($oldName_str,$newName_str))          {                 $this->on_error('Can\'t rename file!',308);                 exit;          }   }/******************刪除檔案*********************//*將filename_str指定的檔案刪除$fileName_str要刪除檔案的路徑和名稱返回錯誤資訊*/   public function delete_file($fileName_str)//   {          if(!unlink($fileName_str))          {                 $this->on_error('Can\'t delete file!',309);//出錯處理                 exit;          }   }/******************取檔案的副檔名*********************//*取filename_str指定的檔案的副檔名$fileName_str要取類型的檔案路徑和名稱返迴文件的副檔名*/   public function get_file_type($fileName_str)   {          $fileNamePart_arr=explode('.',$fileName_str);          while(list(,$fileType_str)=each($fileNamePart_arr))          {           $type_str=$fileType_str;          }           return $type_str;   }/******************判斷檔案是否是規定的檔案類型*********************//*$fileType_str規定的檔案類型$fileName_str要取類型的檔案路徑和名稱返回false或true*/   public function is_the_type($fileName_str,$fileType_arr)   {       $cheakFileType_str=$this->get_file_type($fileName_str);       if(!in_array($cheakFileType_str,$fileType_arr))       {        return false;          }       else       {          return true;       }   }/******************上傳檔案,並返回上傳後的檔案資訊*********************//*$fileName_str本地檔案名稱$filePath上傳檔案的路徑,如果$filePath是str則上傳到同一目錄用一個檔案命名,新檔案名稱在其加-1,2,3..,如果是arr則順序命名$allowType_arr允許上傳的檔案類型,留空不限制$maxSize_int允許檔案的最大值,留空不限制返回的是新檔案資訊的二維數組:$reFileInfo_arr*/   public function upload_file($fileName_str,$filePath,$allowType_arr='',$maxSize_int=''){             $fileName_arr=$_FILES[$fileName_str]['name'];  //檔案的名稱       $fileTempName_arr=$_FILES[$fileName_str]['tmp_name'];  //檔案的快取檔案       $fileSize_arr=$_FILES[$fileName_str]['size'];//取得檔案大小       $reFileInfo_arr=array();       $num=count($fileName_arr)-1;       for($i=0;$i<=$num;$i++)      {           if($fileName_arr[$i]!='')         {          if($allowType_arr!='' and !$this->is_the_type($fileName_arr[$i],$allowType_arr))//判斷是否是允許的檔案類型          {           $this->on_error('The file is not allowed type!',310);//出錯處理           break;          }          if($maxSize_int!='' and $fileSize_arr[$i]>$maxSize_int)          {           $this->on_error('The file is too big!',311);//出錯處理           break;          }            $j=$i+1;          $fileType_str=$this->get_file_type($fileName_arr[$i]);//取得檔案類型          if(!is_array($filePath))          {          $fileNewName_str=$filePath.'-'.($j).'.'.$fileType_str;          }          else          {          $fileNewName_str=$filePath_arr[$i].'.'.$fileType_str;          }          copy($fileTempName_arr[$i],$fileNewName_str);//上傳檔案          unlink($fileTempName_arr[$i]);//刪除快取檔案          //---------------隱藏檔資訊--------------------//          $doFile_arr=explode('/',$fileNewName_str);          $doFile_num_int=count($doFile_arr)-1;          $reFileInfo_arr[$j]['name']=$doFile_arr[$doFile_num_int];          $reFileInfo_arr[$j]['type']=$fileType_str;          $reFileInfo_arr[$j]['size']=$this->change_size_express($fileSize_arr[$i]);      }   }   return $reFileInfo_arr;} }?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.