日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP操作文件的常用函数

發(fā)布時間:2023/12/20 php 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP操作文件的常用函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • fopen() 打開文件或者 URL
    • fsockopen()打開一個網絡連接或者一個Unix套接字連接
    • fwrite () 寫入文件
    • basename() 返回路徑中的文件名部分。
    • copy()復制文件
    • dirname () 返回路徑中的目錄部分
    • disk_free_space () 返回目錄中的可用空間
    • disk_total_space () 返回一個目錄的磁盤總大小
    • fclose 關閉一個已打開的文件指針
    • feof() 測試文件指針是否到了文件結束的位置
    • fgetc 從文件指針中讀取一個字符
    • fgets 從文件指針中讀取一行
    • fgetss() 從打開的文件中讀取一行并過濾掉 HTML 和 PHP 標記。
    • file() 把整個文件讀入一個數組中
    • file_exists ( string $filename ) : bool 檢查文件或目錄是否存在
    • file_get_contents () 將整個文件讀入一個字符串
    • file_put_contents 將一個字符串寫入文件
    • filesize() 函數返回指定文件的大小 ,返回字節(jié)數
    • filetype() 返回指定文件或目錄的類型。
    • flock() 鎖定或釋放文件
    • unlink() 刪除文件
    • fgetcsv() 從文件指針中讀入一行并解析 CSV 字段
    • fputcsv() 將行格式化為 CSV 并寫入文件指針
    • fpassthru() 函數輸出文件指針處的所有剩余數據
    • parse_ini_file() 函數解析一個配置文件,并以數組的形式返回其中的設置。
    • pathinfo() 以數組的形式返回文件路徑的信息
    • realpath() 返回文件的絕對路徑
    • rename() 函數重命名文件或目錄
    • mkdir() 創(chuàng)建目錄
    • move_uploaded_file() 將上傳的文件移動到新位置
    • rmdir() 刪除空目錄
    • touch() 設置指定文件的訪問和修改時間
    • unlink() 刪除文件
    • fseek() 在打開的文件中定位
    • rewind() 將文件指針的位置倒回文件的開頭。
    • glob() 尋找與模式匹配的文件路徑
    • scandir() 列出指定路徑中的文件和目錄
    • opendir() 打開目錄
    • readdir() 獲取打開目錄中的一條子目錄/文件名稱
    • closedir(); 關閉目錄
    • is_dir() 判斷指定的文件名是否是一個目錄。
    • is_executable() 判斷文件是否可執(zhí)行。
    • is_file() 判斷指定文件是否為常規(guī)的文件。
    • is_link() 判斷指定的文件是否是連接。
    • is_readable() 判斷文件是否可讀。
    • is_uploaded_file() 判斷文件是否是通過 HTTP POST 上傳的。
    • is_writable() 判斷文件是否可寫。
    • 統計指定文件夾下的所有文件跟目錄數

demo.txt 內容:

this is demo.txt 1 this is demo.txt 2 this is demo.txt 3 this is demo.txt 4 this is demo.txt 5 this is demo.txt 6 this is demo.txt 7 this is demo.txt 8 this is demo.txt 9

fopen() 打開文件或者 URL

fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] ) : resource

$handle = fopen("/home/rasmus/file.txt", "r"); $handle = fopen("/home/rasmus/file.gif", "wb"); $handle = fopen("http://www.example.com/", "r"); $handle = fopen("ftp://user:password@example.com/somefile.txt", "w");

fsockopen()打開一個網絡連接或者一個Unix套接字連接

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) {echo "$errstr ($errno)<br />\n"; } else {$out = "GET / HTTP/1.1\r\n";$out .= "Host: www.example.com\r\n";$out .= "Connection: Close\r\n\r\n";fwrite($fp, $out);while (!feof($fp)) {echo fgets($fp, 128);}fclose($fp); }$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr); if (!$fp) {echo "ERROR: $errno - $errstr<br />\n"; } else {fwrite($fp, "\n");echo fread($fp, 26);fclose($fp); }

fwrite () 寫入文件

fwrite ( resource $handle , string $string [, int $length ] ) : int

$file = fopen("test.txt","w"); echo fwrite($file,"Hello World. Testing!"); fclose($file);

basename() 返回路徑中的文件名部分。

echo "1) ".basename("/etc/sudoers.d").PHP_EOL; echo "2) ".basename("/etc/sudoers.d", ".d").PHP_EOL; echo "3) ".basename("/etc/passwd").PHP_EOL; echo "4) ".basename("/etc/").PHP_EOL; echo "5) ".basename(".").PHP_EOL; echo "6) ".basename("/"); /* 1) sudoers.d 2) sudoers 3) passwd 4) etc 5) . 6)*/

copy()復制文件

copy ( string $source , string $dest [, resource $context ] ) : bool echo copy("要復制的文件路徑/鏈接.txt","target.txt");//bool(true/false)

dirname () 返回路徑中的目錄部分

dirname ( string $path ) : string echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or \ on Windows) echo "3) " . dirname("."); // 3) .

disk_free_space () 返回目錄中的可用空間

disk_free_space ( string $directory ) : float echo disk_free_space('c:');//37684822016 返回可用的字節(jié)數。

disk_total_space () 返回一個目錄的磁盤總大小

disk_total_space ( string $directory ) : float echo disk_total_space('c:');//118409187328 字節(jié)數

fclose 關閉一個已打開的文件指針

fclose ( resource $handle ) : bool $handle = fopen('somefile.txt', 'r'); fclose($handle);

feof() 測試文件指針是否到了文件結束的位置

feof ( resource $handle ) : bool $file = fopen("demo.txt", "r");//輸出文本中所有的行,直到文件結束為止。 while(!feof($file)) {echo fgets($file); }fclose($file);

fgetc 從文件指針中讀取一個字符

fgetc ( resource $handle ) : string $file = fopen("demo.txt", "r"); echo fgetc($file);//t

fgets 從文件指針中讀取一行

fgets ( resource $handle [, int $length ] ) : string $file = fopen("demo.txt", "r"); echo fgets($file,2048);//this is demo.txt 1

fgetss() 從打開的文件中讀取一行并過濾掉 HTML 和 PHP 標記。

fgetss ( resource $handle [, int $length [, string $allowable_tags ]] ) : string demo.txt <div><p><span>this is demo.txt 1</span></p></div>demo.php $file = fopen("demo.txt", "r"); echo fgetss($file,1024,'<span><p>'); //<p><span>this is demo.txt 1</span></p>

file() 把整個文件讀入一個數組中

file ( string $filename [, int $flags = 0 [, resource $context ]] ) : array

demo.txt <div><p><span>this is demo.txt 1</span></p></div> this is demo.txt 2 this is demo.txt 3 this is demo.txt 4 this is demo.txt 5 this is demo.txt 6this is demo.txt 7 this is demo.txt 8 this is demo.txt 9print_r( file('demo.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)); /* Array ([0] => <div><p><span>this is demo.txt 1</span></p></div>[1] => this is demo.txt 2[2] => this is demo.txt 3[3] => this is demo.txt 4[4] => this is demo.txt 5[5] => this is demo.txt 6[6] => this is demo.txt 7[7] => this is demo.txt 8[8] => this is demo.txt 9 )*/

file_exists ( string $filename ) : bool 檢查文件或目錄是否存在

file_get_contents () 將整個文件讀入一個字符串

file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] ) : string echo file_get_contents('demo.txt'); /*<div><p><span>this is demo.txt 1</span></p></div> this is demo.txt 2 this is demo.txt 3 this is demo.txt 4 this is demo.txt 5 this is demo.txt 6this is demo.txt 7 this is demo.txt 8 this is demo.txt 9 */ echo file_get_contents('demo.txt',false,null,1,10);//div><p><sp

file_put_contents 將一個字符串寫入文件

file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] ) : int 該函數將返回寫入到文件內數據的字節(jié)數,失敗時返回FALSE

echo file_put_contents('demo.txt','this is yzx',FILE_APPEND);

filesize() 函數返回指定文件的大小 ,返回字節(jié)數

filetype() 返回指定文件或目錄的類型。

flock() 鎖定或釋放文件

flock( resource $handle , int $operation [, int &$wouldblock ] ) : bool

$file= fopen("demo.txt", "r+"); // 排它性的鎖定 if (flock($file,LOCK_EX )) {fwrite($file,"Write something fsdfsadf");// release lockflock($file,LOCK_UN);echo 111; } else {echo "Error locking file!"; }fclose($file);

unlink() 刪除文件

若成功,則返回 true,失敗則返回 false。

fgetcsv() 從文件指針中讀入一行并解析 CSV 字段

fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = ',' [, string $enclosure = '"' [, string $escape = '\\' ]]]] ) : array

$file = fopen("demo.csv","r"); print_r(fgetcsv($file)) ; fclose($file);die;

fputcsv() 將行格式化為 CSV 并寫入文件指針

fputcsv ( resource $handle , array $fields [, string $delimiter = ',' [, string $enclosure = '"' ]] ) : int

$list = array (array('aaa', 'bbb', 'ccc', 'dddd'),array('123', '456', '789'),array('"aaa"', '"bbb"') );$fp = fopen('file.csv', 'w');foreach ($list as $fields) {fputcsv($fp, $fields); }fclose($fp);

fpassthru() 函數輸出文件指針處的所有剩余數據

$file = fopen("demo.txt","r");// 讀取第一行 fgets($file);// 把文件的其余部分發(fā)送到輸出緩存 echo fpassthru($file);fclose($file);

parse_ini_file() 函數解析一個配置文件,并以數組的形式返回其中的設置。

test.ini [names] me = Robert you = Peter[urls] first = "http://www.example.com" second = "http://www.w3school.com.cn"demo.php print_r(parse_ini_file("test.ini")); /* Array ( [me] => Robert [you] => Peter [first] => http://www.example.com [second] => http://www.w3school.com.cn ) */ print_r(parse_ini_file("test.ini",true)); Array ( [names] => Array([me] => Robert[you] => Peter) [urls] => Array([first] => http://www.example.com[second] => http://www.w3school.com.cn) )

pathinfo() 以數組的形式返回文件路徑的信息

var_dump(pathinfo('http://blog.yaozhixiong.top/php/index.php')); array(4) {'dirname' =>string(31) "http://blog.yaozhixiong.top/php"'basename' =>string(9) "index.php"'extension' =>string(3) "php"'filename' =>string(5) "index" }

realpath() 返回文件的絕對路徑

rename() 函數重命名文件或目錄

mkdir() 創(chuàng)建目錄

若成功,則返回 true,否則返回 false。

move_uploaded_file() 將上傳的文件移動到新位置

若成功,則返回 true,否則返回 false。

rmdir() 刪除空目錄

只能刪除空的目錄 且不能遞歸刪除

touch() 設置指定文件的訪問和修改時間

文件不存在會被創(chuàng)建

unlink() 刪除文件

若成功,則返回 true,失敗則返回 false。

fseek() 在打開的文件中定位

$file = fopen("demo.txt","r");fseek($file,3); // 讀取第一行 echo fgets($file);//v><p><span>this is demo.txt 1</span></p></div>

rewind() 將文件指針的位置倒回文件的開頭。

$file = fopen("demo.txt","r");fseek($file,3); // 讀取第一行 echo fgets($file);//v><p><span>this is demo.txt 1</span></p></div> echo fgets($file);//this is demo.txt 2 rewind($file); echo fgets($file);//<div><p><span>this is demo.txt 1</span></p></div>

若成功,則返回 true。若失敗,則返回 false。

glob() 尋找與模式匹配的文件路徑

rint_r(glob("*.txt")); Array ( [0] => target.txt [1] => source.txt [2] => test.txt [3] => test2.txt )

scandir() 列出指定路徑中的文件和目錄

scandir ( string $directory [, int $sorting_order [, resource $context ]] ) : array

opendir() 打開目錄

readdir() 獲取打開目錄中的一條子目錄/文件名稱

closedir(); 關閉目錄

is_dir() 判斷指定的文件名是否是一個目錄。

is_executable() 判斷文件是否可執(zhí)行。

is_file() 判斷指定文件是否為常規(guī)的文件。

is_link() 判斷指定的文件是否是連接。

is_readable() 判斷文件是否可讀。

is_uploaded_file() 判斷文件是否是通過 HTTP POST 上傳的。

is_writable() 判斷文件是否可寫。

統計指定文件夾下的所有文件跟目錄數

$numdir = $numfile = 0; function foo ($dir) {global $numdir,$numfile;foreach (scandir($dir) as $key => $val){if ($val !== '.' && $val !== '..'){if ( is_dir($file = $dir.'/'.$val)){++$numdir;//目錄數foo($file);}if ( is_file($dir.'/'.$val)){++$numfile;//文件數量}}} } foo('laravel'); echo '目錄數:'.$numdir.PHP_EOL; echo '文件數:'.$numfile;

總結

以上是生活随笔為你收集整理的PHP操作文件的常用函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。