最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP編程文件處理類SplFileObject和SplFileInfo用法實(shí)例分析

 更新時(shí)間:2017年07月22日 11:18:15   作者:3wlog  
這篇文章主要介紹了PHP編程文件處理類SplFileObject和SplFileInfo用法,結(jié)合實(shí)例形式分析了文件處理類SplFileObject和SplFileInfo的功能、定義、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了PHP編程文件處理類SplFileObject和SplFileInfo用法。分享給大家供大家參考,具體如下:

php對(duì)于大文件的處理除了用以下方法外還可以直接調(diào)用linux命令

文件處理類:

SplFileInfo {
/* 方法 */
public __construct ( string $file_name )
public int getATime ( void )   //獲取文件的最后訪問時(shí)間
public string getBasename ([ string $suffix ] ) //獲取文件的沒有路徑信息的基本名稱,參數(shù)可以為文件后綴,若有參數(shù)則返回沒有該后綴的文件基本名稱。
public int getCTime ( void )    //返回文章最后一次變更的時(shí)間戳。
public string getExtension ( void )  //獲取文件擴(kuò)展名
public SplFileInfo getFileInfo ([ string $class_name ] ) //以對(duì)象的形式返回文件路徑和名稱
public string getFilename ( void )  //獲取文件名稱,不帶路徑
public int getGroup ( void )  //獲取文件所在組,返回組id
public int getInode ( void )  //獲取文件索引節(jié)點(diǎn)
public string getLinkTarget ( void ) //獲取文件鏈接目標(biāo)
public int getMTime ( void ) //獲取最后修改時(shí)間
public int getOwner ( void )   //獲取文件的所有者
public string getPath ( void )  //獲取文件路徑,不帶文件名和最后的斜杠
public SplFileInfo getPathInfo ([ string $class_name ] )  //返回路徑對(duì)象
public string getPathname ( void )  //獲取文件路徑
public int getPerms ( void )  //獲取文件權(quán)限
public string getRealPath ( void ) //獲取文件絕對(duì)路徑,若文件不存在,返回false
public int getSize ( void )  //返回文件大小,單位字節(jié)
public string getType ( void ) //返回文件類型,可能是 file, link, dir
public bool isDir ( void ) //判斷是否是目錄,是放回true否則返回false
public bool isExecutable ( void ) //判斷文件是否可執(zhí)行,返回true,否則返回false
public bool isFile ( void )  //如果文件存在且是一個(gè)普通文件(不是鏈接),返回true,否則返回false
public bool isLink ( void ) //判斷文件是否是連接,不是返回false
public bool isReadable ( void )  //判斷文件是否可讀,可讀返回true
public bool isWritable ( void )  //判斷文件是否可寫,可寫返回true
public SplFileObject openFile ([ string $open_mode = "r" [, bool $use_include_path = false [, resource $context = NULL ]]] ) //獲取文件對(duì)象信息
public void setFileClass ([ string $class_name = "SplFileObject" ] )
public void setInfoClass ([ string $class_name = "SplFileInfo" ] )
public void __toString ( void )  //以字符串的形式返回文件路徑及名稱
}

使用方法:

$info = new SplFileInfo($file_name);

SplFileObject {
/* 常量 */
const integer DROP_NEW_LINE = 1 ;
const integer READ_AHEAD = 2 ;
const integer SKIP_EMPTY = 4 ;
const integer READ_CSV = 8 ;
/* 方法 */
public string|array current ( void )   //返回文件當(dāng)前行內(nèi)容
public bool eof ( void ) //檢測(cè)文件是否到末尾,如果到末尾返回true,否則返回false
public bool fflush ( void )  //將緩沖內(nèi)容輸出到文件,成功時(shí)返回 TRUE, 或者在失敗時(shí)返回 FALSE。
public string fgetc ( void )  //按字符讀取文件
public array fgetcsv ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] )  //讀取csv文件
public string fgets ( void )  //按行讀取文件
public string fgetss ([ string $allowable_tags ] ) //按行讀取文件,并去掉html標(biāo)記
public bool flock ( int $operation [, int &$wouldblock ] )  //文件鎖定或解鎖,返回true或false
/*參數(shù):
LOCK_SH 共享鎖 (讀).
LOCK_EX 獨(dú)占鎖 (寫).
LOCK_UN 釋放鎖 (共享或獨(dú)占).
LOCK_NB (附加鎖定) 如果不希望flock()在鎖定時(shí)堵塞,則應(yīng)在上述鎖定后加上改鎖(windows上不支持)
flock(LOCK_EX+LOCK_NB);  // 獨(dú)占鎖定加上附加鎖定
flock(LOCK_UN+LOCK_NB); */
public int fpassthru ( void )  //輸出文件指針之后的所有數(shù)據(jù)和字符數(shù)
public int fputcsv (array $fields) //將一維數(shù)組作為一行輸入csv文件中,返回寫入的字符串長(zhǎng)度或false
public string fread ( int $length ) //從文件中讀取指定的字節(jié)數(shù),返回讀取的字符串或false
public mixed fscanf ( string $format [, mixed &$... ] ) //從文件中讀取一行并按照指定模式解析
/*例:
$file = new SplFileObject("misc.txt");
while ($userinfo = $file->fscanf("%s %s %s")) {
  list ($name, $profession, $countrycode) = $userinfo;
  // Do something with $name $profession $countrycode
}*/
public int fseek ( int $offset [, int $whence = SEEK_SET ] ) //按字節(jié)移動(dòng)文件指針位置,
/*SEEK_SET 設(shè)置文件指針到指定字節(jié)位置(默認(rèn)為該模式).
SEEK_CUR 設(shè)置文件指針到當(dāng)前位置加上指定字節(jié)位置.
SEEK_END 設(shè)置文件指針到文件末尾加上指定字節(jié)位置(此時(shí)字節(jié)經(jīng)常為負(fù)值).
public array fstat ( void ) //獲取文件信息,以數(shù)組形式返回*/
/*
數(shù)字下標(biāo)  關(guān)聯(lián)鍵名(自 PHP 4.0.6)  說明
0      dev device number - 設(shè)備名
1      ino inode number - inode 號(hào)碼
2      mode  inode protection mode - inode 保護(hù)模式
3      nlink  number of links - 被連接數(shù)目
4      uid userid of owner - 所有者的用戶 id
5      gid groupid of owner- 所有者的組 id
6      rdev  device type, if inode device * - 設(shè)備類型,如果是 inode 設(shè)備的話
7      size  size in bytes - 文件大小的字節(jié)數(shù)
8      atime  time of last access (unix timestamp) - 上次訪問時(shí)間(Unix 時(shí)間戳)
9      mtime  time of last modification (unix timestamp) - 上次修改時(shí)間(Unix 時(shí)間戳)
10     ctime  time of last change (unix timestamp) - 上次改變時(shí)間(Unix 時(shí)間戳)
11     blksize blocksize of filesystem IO * - 文件系統(tǒng) IO 的塊大小
12     blocks number of blocks allocated - 所占據(jù)塊的數(shù)目* Windows 下總是 0。
* - 僅在支持 st_blksize 類型的系統(tǒng)下有效。其它系統(tǒng)(如 Windows)返回 -1。 */
public int ftell ( void ) //返回當(dāng)前文件位置,文件指針位置
public bool ftruncate ( int $size )  //將文件截?cái)嗟街付ǖ拈L(zhǎng)度,若長(zhǎng)度大于文件長(zhǎng)度用空補(bǔ)齊(文件打開方法對(duì)其有影響)
public int fwrite ( string $str [, int $length ] ) //將$str字符串寫入文件,只寫$length長(zhǎng)度。放回寫入字節(jié)數(shù)或null
public array getCsvControl ( void ) //Gets the delimiter and enclosure character used for parsing CSV fields.
public int getFlags ( void ) //Gets the flags set for an instance of SplFileObject as an integer.
public int getMaxLineLen ( void ) //返回一行讀取的最大字節(jié)數(shù)(在已設(shè)置的前提下),若未設(shè)置,默認(rèn)為0
public int key ( void ) //獲取當(dāng)前行號(hào)。
public void next ( void ) //移動(dòng)到下一行
public void rewind ( void ) //返回到第一行
public void seek ( int $line_pos ) //定位到文件指定行
public void setCsvControl ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] )
public void setFlags ( int $flags )
public void setMaxLineLen ( int $max_len ) //設(shè)置文件讀取一行的最大字節(jié)數(shù),若文件每行有10個(gè)字符,但設(shè)置最大讀取為
public bool valid ( void ) //檢查是否到達(dá)文件底部,未到達(dá)底部返回 TRUE ,抵達(dá)返回false.
}

用法:

$file = new SplFileObject("misc.txt", 'r+');
while (!$file->eof()) {
  echo $file->current();
  $file->next();
}
//關(guān)閉文件對(duì)象
$file = null;

常用文件處理方法:

/**
 * 獲取文件指定行數(shù)范圍數(shù)據(jù)
 * @param unknown $filename 文件名稱
 * @param number $startLine 開始行
 * @param number $endLine  結(jié)束行
 * @param string $method
 * @return multitype:
 */
function getFileLines($filename, $startLine = 1, $endLine = 20, $method = 'rb'){
  $content = array();
  $count = $endLine - $startLine;
  $fp = new SplFileObject($filename, $method);
  $fp->seek($startLine - 1); // 轉(zhuǎn)到第N行, seek方法參數(shù)從0開始計(jì)數(shù)
  for ($i = 0; $i <= $count; ++$i) {
    $content[] = $fp->current(); // current()獲取當(dāng)前行內(nèi)容
    $fp->next(); // 下一行
    if($fp->eof()) {
      array_pop($content);
      break;
    }
  }
  return array_filter($content); // array_filter過濾:false,null,''
}
/**
 * 獲取文章最后一行內(nèi)容
 * @param string $res 文件路徑/名
 */
function get_last_line($res) {
  $fp = fopen($res, 'r');
  if (false == $fp) {
    return 'error';
  }
  fseek($fp,-1,SEEK_END);
  $s = '';
  while(($c = fgetc($fp)) !== false)
  {
    if($c == "\n" && $s) break;
    $s = $c . $s;
    fseek($fp, -2, SEEK_CUR);
  }
  fclose($fp);
  return $s;
}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php實(shí)現(xiàn)快速排序法函數(shù)代碼

    php實(shí)現(xiàn)快速排序法函數(shù)代碼

    取一個(gè)值與其他值進(jìn)行比較,小的放在這個(gè)值的左邊,大的放在這個(gè)值的右邊,然后按照這個(gè)方式遞歸
    2012-08-08
  • FastCGI 進(jìn)程意外退出造成500錯(cuò)誤

    FastCGI 進(jìn)程意外退出造成500錯(cuò)誤

    這篇文章主要介紹了FastCGI 進(jìn)程意外退出造成500錯(cuò)誤的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • phpmyadmin打開很慢的解決方法

    phpmyadmin打開很慢的解決方法

    這篇文章主要介紹了phpmyadmin打開很慢的解決方法,一般這個(gè)問題的原因是phpmyadmin的版本檢測(cè)造成的,因?yàn)閜hpmyadmin的官網(wǎng)有時(shí)會(huì)打不開,這時(shí)只要取消代碼中的版本檢測(cè)即可解決這個(gè)問題,需要的朋友可以參考下
    2014-04-04
  • 初識(shí)php MVC

    初識(shí)php MVC

    MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫,一種軟件設(shè)計(jì)典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個(gè)部件里面,在改進(jìn)和個(gè)性化定制界面及用戶交互的同時(shí),不需要重新編寫業(yè)務(wù)邏輯。
    2014-09-09
  • PHP 創(chuàng)建文件(文件夾)以及目錄操作代碼

    PHP 創(chuàng)建文件(文件夾)以及目錄操作代碼

    PHP 創(chuàng)建文件(文件夾)以及目錄操作代碼,需要的朋友可以參考下。
    2010-03-03
  • 用php來改寫404錯(cuò)誤頁(yè)讓你的頁(yè)面更友好

    用php來改寫404錯(cuò)誤頁(yè)讓你的頁(yè)面更友好

    404錯(cuò)誤,很多人都知道,如果要訪問的url不存在的時(shí)候就讀取顯示這個(gè)頁(yè)面;以往在處理404寫幾行字,有心人對(duì)其稍加美化,首先我來說明一下我的404究竟都能幫我做哪些事情以及PHP如何來改寫你的404錯(cuò)誤頁(yè)
    2013-01-01
  • php+mysql數(shù)據(jù)庫(kù)查詢實(shí)例

    php+mysql數(shù)據(jù)庫(kù)查詢實(shí)例

    這篇文章主要介紹了php+mysql數(shù)據(jù)庫(kù)查詢的方法,實(shí)例分析了數(shù)據(jù)庫(kù)查詢的原理與完整實(shí)現(xiàn)步驟,并進(jìn)行了針對(duì)性的分析說明,需要的朋友可以參考下
    2015-01-01
  • php使用curl偽造瀏覽器訪問操作示例

    php使用curl偽造瀏覽器訪問操作示例

    這篇文章主要介紹了php使用curl偽造瀏覽器訪問操作,結(jié)合實(shí)例形式分析了php使用curl偽造瀏覽器訪問的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-09-09
  • PHP讀取配置文件類實(shí)例(可讀取ini,yaml,xml等)

    PHP讀取配置文件類實(shí)例(可讀取ini,yaml,xml等)

    這篇文章主要介紹了PHP讀取配置文件類,可讀取ini,yaml,xml等配置文件,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • php實(shí)現(xiàn)的操作excel類詳解

    php實(shí)現(xiàn)的操作excel類詳解

    這篇文章主要介紹了php實(shí)現(xiàn)的操作excel類,較為詳細(xì)的分析說明了PHP操作excel的具體技巧,包括PHP針對(duì)excel的創(chuàng)建、打開、讀取、修改等,需要的朋友可以參考下
    2016-01-01

最新評(píng)論

开鲁县| 德昌县| 准格尔旗| 从江县| 通城县| 麟游县| 平谷区| 河源市| 长沙县| 石柱| 台北市| 和田县| 开阳县| 建宁县| 麻栗坡县| 崇明县| 炎陵县| 榆林市| 威远县| 金山区| 拉孜县| 崇文区| 凌海市| 竹山县| 喀喇沁旗| 阳东县| 德昌县| 同德县| 丹江口市| 潞西市| 石屏县| 尼勒克县| 章丘市| 广宁县| 桃园市| 文成县| 会昌县| 德格县| 德庆县| 泗水县| 余庆县|