PHP文件操作類

<?php
/***************************************************************************************
文件名:File.cls.php
文件簡介:類clsFile的定義,對文件操作的封裝
版本:2.0  最后修改日期:2011-8-23
****************************************************************************************/
!defined('INIT_PHPV') && die('No direct script access allowed');
class clsFile
{private $fileName_str;         //文件的路徑private $fileOpenMethod_str;   //文件打開模式function __construct($fileName_str='',$fileOpenMethod_str='readOnly')//路徑,默認為空;模式,默認均為只讀
   {//構造函數,完成數據成員的初始化$this->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_intif(!$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,不存在返回falsereturn 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;
}/******************備份文件夾*********************/
}?>

?

轉載于:https://www.cnblogs.com/joshua317/articles/4547136.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258817.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258817.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258817.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

excel打開后灰色不顯示內容_Excel二維表轉換,一分鐘就夠

點擊藍字關注我們44個Excel 使用技巧基本方法作為職場人&#xff0c;加班累如狗。如何更輕松的工作并獲得喜人的報酬便是咱們職場人一直追求的“生活哲理”&#xff0c;說到Excel,對于辦公室群體而言實在是太常見不過了&#xff0c;不管做什么&#xff0c;咱們都會跟它打交道&a…

利用PHP SOAP實現web service

一 什么是SOAP&#xff1f;可以做什么&#xff1f; SOAP 指簡單對象訪問協議&#xff0c;它是一種基于XML的消息通訊格式&#xff0c;用于網絡上&#xff0c;不同平臺&#xff0c;不同語言的應用程序間的通訊。可自定義&#xff0c;易于擴展。一條 SOAP 消息就是一個普通的 XML…

UVA350-水題

UVA350-水題 #include<iostream>using namespace std;int main() {int c 0;int Z, L, I, M;while (cin >> Z >> I >> M >> L){c;if(Z L && L I && I M && M 0){return 0;}int i 1;int K, P;I I % M;Z Z % M;K …

卷積的循環矩陣求解方法

通常我們求解一維卷積或者二維卷積都是采用模板平移的方法&#xff0c;今天我們介紹一種新的求解方法&#xff0c;可以一次性求出所有的結果。 一維卷積 卷積定義 對于兩個長度分別為m和n的序列x(i)和g(i)有&#xff0c; h(i)x(i)?g(i)∑jx(j)g(i?j)h(i)=x(i)*g(i)=\sum_…

Windows 10 開發日記(五)-- 當Binding遇到異步 -- 解決方案

前文再續&#xff0c;上一章提出了問題&#xff0c;本章提出了三種解決方案&#xff1a; 解決方案一&#xff1a;手動進行異步轉換,核心思想:將binding做的事情放入CodeBehind FilterItemControl.XAML: <Grid><Image x:Name"FilterImage" Stretch"Unif…

fseek

int fseek( FILE *stream, long offset, int origin );第一個參數stream為文件指針第二個參數offset為偏移量&#xff0c;正數表示正向偏移&#xff0c;負數表示負向偏移第三個參數origin設定從文件的哪里開始偏移,可能取值為&#xff1a;SEEK_CUR、 SEEK_END 或 SEEK_SETSEEK_…

static_cast, dynamic_cast, const_cast探討【轉】

首先回顧一下C類型轉換&#xff1a; C類型轉換分為&#xff1a;隱式類型轉換和顯式類型轉換 第1部分. 隱式類型轉換又稱為“標準轉換”&#xff0c;包括以下幾種情況&#xff1a;1) 算術轉換(Arithmetic conversion) : 在混合類型的算術表達式中, 最寬的數據類型成為目標轉換類…

RANSAC算法注記

今天學習了一下RANSAC隨機樣本一致性算法&#xff0c;其在圖像融合、特征點匹配方面有很強大的應用。網上已經有很多人寫了關于這方面的文檔&#xff0c;就不再造輪子了。特此羅列出來&#xff0c;以供后續參考。 我的數學之美&#xff08;一&#xff09;——RANSAC算法詳解 …

python字典格式_python – 格式self,這是一個字典

在這種情況下如何使格式(自我)工作&#xff1f;class Commit:number Nonesha Nonemessage Noneidentity Nonedef __init__(self, raw, number):r raw.commits[number]self.number numberself.sha r[sha]self.message r[message]self.identity raw.identities[r[identi…

委托的BeginInvoke和EndInvoke

剛剛搞明白了C#的異步調用&#xff0c;寫下來&#xff0c;方便后續調用。 異步主要是解決UI假死的問題&#xff0c;而開辟出一個新的線程&#xff0c;處理大數據。 1.既然是委托的調用&#xff0c;那么先定義個委托&#xff1a; public delegate bool CheckUpdateFile(); 2.定義…

PMP 第七章 項目成本管理

估算成本 制定預算 控制成本 1.成本管理計劃的內容和目的是什么? 包括對成本進行估算 預算和控制的各過程&#xff0c;從而確保項目在批準的預算內完工。 2.直接成本、間接成本、可變成本、固定成本、質量成本的內容分別是什么?成本估算的工具有哪些? 成本估算工具 1…

您的請求參數與訂單信息不一致_[淘客訂單檢測]淘寶客訂單檢測接口,淘客訂單查詢API...

功能1.輸入交易的訂單編號&#xff0c;即可查詢該訂單是否為淘寶客訂單。有意向請聯系衛星weixiaot168。2.查詢結果 0:不是淘寶客訂單&#xff1b;1:是。3.根據淘寶官方的后臺數據&#xff0c;進行檢測&#xff0c;數據真實且有效。4.有效防止傭金損失&#xff0c;降低商家補單…

DebugView輸出調試信息

在寫windows程序時&#xff0c;需要輸出一些調試信息&#xff0c;這里介紹一種極其方便的方法。即使用OutputDebugString 在Debug模式下輸出調試信息&#xff0c;在Release模式下不輸出。 我們可以在VS的集成平臺上輸出調試信息&#xff0c;也可以使用DebugView來查看調試信息…

Linux上實現ssh免密碼登陸遠程服務器

0.說明平常使用ssh登陸遠程服務器時&#xff0c;都需要使用輸入密碼&#xff0c;希望可以實現通過密鑰登陸而免除輸入密碼&#xff0c;從而可以為以后實現批量自動部署主機做好準備。環境如下&#xff1a;IP地址操作系統服務器端10.0.0.128/24CentOS 6.5 x86客戶端10.0.0.129/2…

【強連通分量+概率】Bzoj2438 殺人游戲

Description 一位冷血的殺手潛入 Na-wiat&#xff0c;并假裝成平民。警察希望能在 N 個人里面&#xff0c;查出誰是殺手。 警察能夠對每一個人進行查證&#xff0c;假如查證的對象是平民&#xff0c;他會告訴警察&#xff0c;他認識的人&#xff0c; 誰是殺手&#xff0c; 誰是…

serialversionuid的作用_為什么阿里Java規約要求謹慎修改serialVersionUID字段

serialVersionUID簡要介紹serialVersionUID是在Java序列化、反序列化對象時起作用的一個字段。Java的序列化機制是通過判斷類的serialVersionUID來驗證版本一致性的。在進行反序列化時&#xff0c;JVM會把傳來的字節流中的serialVersionUID與本地相應實體類的serialVersionUID進…

fatal error LNK1169: 找到一個或多個多重定義的符號 的解決方案

昨天&#xff0c;嘗試一個項目&#xff0c;遇到了如下的問題。先來還原一下&#xff1a; 頭文件test.h #pragma once #include <Eigen/Core> #include <iostream>using namespace Eigen; using namespace std;class point2 { public: point2(int x1,int y1):x(x…

常用工具說明--搭建基于rietveld的CodeReview平臺(未測試)

為什么要codereview . 整個團隊的編碼風格是統一的。 . 有高手能對自己的代碼指點一二&#xff0c;從而提高編碼水平。 . 減少低級錯誤的出現 . 約束自己寫高質量的代碼&#xff0c;因為是要給人看的。 我們對codereview的需求 . 很輕松可以發布自己寫的代碼。 . 很輕松的可以與…

輸入的優化

讀入整型時&#xff0c;輸入優化可以節省不少時間 1 typedef type long long 2 // 這里以long long為例 3 type read() { 4 type x0; int f1; 5 char chgetchar(); 6 while(ch<0||ch>9) {if(ch-) f-1; chgetchar();} 7 while(ch>0&&ch<9) …

python股票分析系統_熬了一晚上,小白用Python寫了一個股票提醒系統

碼農小馬七夕節去相親了&#xff0c;見了一個不錯的姑娘&#xff0c;長的非常甜美&#xff01;聊著聊著很投緣&#xff01;通過介紹人了解到&#xff0c;對方也很滿意&#xff5e;&#xff5e;想著自己單身多年的生活就要結束啦&#xff0c;心里滿是歡喜&#xff0c;美美噠&…