Summary of commonly used file operation functions in PHP
Apr 03, 2024 pm 02:52 PM目錄
- 1 :basename()
- 2 :copy()
- 3 :dirname()
- 4 :disk_free_space()
- 5 :disk_total_space()
- 6 :file_exists()
- 7 :file_get_contents()
- 8 :file_put_contents()
- 9 :filesize()
- 10 :filetype()
- 11 :glob()
- 12 :is_dir()
- 13 :is_writable()
- 14 :mkdir()
- 15 :move_uploaded_file()
- 16 :parse_ini_file()
- 17 :realpath()
- 18 :rename()
- 19 :tempnam()
- 20 :tmpfile()
- 21:unlink()
- 22 :chmod()
- 23:chown()
- 24:chgrp()
php小編柚子精心整理了Summary of commonly used file operation functions in PHP,為廣大PHP開發(fā)者提供了一份實用的文件操作函數(shù)參考指南。通過本文的總結(jié),讀者可以快速了解PHP中文件操作的各種常用函數(shù),提高文件處理的效率和準(zhǔn)確性。無論是對于初學(xué)者還是有一定開發(fā)經(jīng)驗的開發(fā)者,都能從文章中獲得實用的技術(shù)知識和經(jīng)驗。
注:文件操作函數(shù)的行為受到 php.ini 中設(shè)置的影響。
當(dāng)在 Unix 平臺上規(guī)定路徑時,正斜杠 (/) 用作目錄分隔符。而在 windows 平臺上,正斜杠 (/) 和反斜杠 () 均可使用。
1 :basename()
返回路徑中的文件名。分為帶擴展名和不帶擴展名的。
語法:basename(path,suffix)
Path:必需。規(guī)定要檢查的路徑。
Suffix:可選。規(guī)定文件擴展名。如果文件有名有文件擴展名,將不會顯示這個擴展名。
// basename $path = "/testWEB/home.php"; // 輸出文件名,包含擴展名 echo basename($path) ."<br/>";// home.php // 輸出文件名,不包含擴展名 echo basename($path,".php");// home
2 :copy()
復(fù)制文件。該函數(shù)如果成功則返回 TRUE,如果失敗則返回 FALSE。如果目標(biāo)文件已存在,將會被覆蓋。
語法:copy(file,to_file)
File:必需。規(guī)定要復(fù)制的文件。
to_file:必需。規(guī)定復(fù)制文件的目的地。
// 復(fù)制文件,返回值為bool echo copy("source.txt","target.txt");
3 :dirname()
返回路徑中的目錄部分。
語法:dirname(path)
Path:必需。規(guī)定要檢查的路徑。
// 返回文件路徑 echo dirname("c:/testweb/home.php")."<br>";// c:/testweb/ echo dirname("/testweb/home.php");// /testweb/
4 :disk_free_space()
返回目錄的可用空間。,以字節(jié)為單位。
語法:disk_free_space(directory)
Directory:必需。規(guī)定要檢查的目錄。(該目錄有限制)
// 返回指定目錄的可用空間(查詢的目錄是有限制的) echo disk_free_space("D:/wwwroot/xxx.com/");
5 :disk_total_space()
返回一個目錄的磁盤總?cè)萘俊7祷刈止?jié)數(shù)
語法:disk_total_space(directory)
Directory:必需。規(guī)定要檢查的目錄。
echo disk_total_space("C:/Windows/Temp/"); echo "<hr>";
6 :file_exists()
檢查文件或目錄是否存在。返回bool值
語法:file_exists(path)
Path:必需。規(guī)定要檢查的路徑。
// 查看test.txt是否存在,返回bool值 echo file_exists("target.txt"); // 1 echo "<hr>";
7 :file_get_contents()
將文件讀入字符串。
語法:file_get_contents(path,include_path,context,start,max_length)
Path:必需。規(guī)定要讀取的文件。
include_path:可選。如果您還想在 include_path(在 php.ini 中)中搜索文件的話,請設(shè)置該參數(shù)為 '1'。
Context:可選。規(guī)定文件句柄的環(huán)境。context 是一套可以修改流的行為的選項。若使用 NULL,則忽略。
Start:可選。規(guī)定在文件中開始讀取的位置。該參數(shù)是 PHP 5.1 中新增的。
max_length:可選。規(guī)定讀取的字節(jié)數(shù)。該參數(shù)是 PHP 5.1 中新增的。
// 讀取文件 echo file_get_contents("target.txt"); echo "<hr>";
提示: 該函數(shù)是二進制安全的。(意思是二進制數(shù)據(jù)(如圖像)和字符數(shù)據(jù)都可以使用此函數(shù)寫入。)
8 :file_put_contents()
將字符串寫入文件。如果成功,該函數(shù)將返回寫入文件中的字符數(shù)。如果失敗,則返回 False。
語法:int file_put_contents ( string filename,mixedfilename , mixed filename,mixeddata [, int flags=0[,resourceflags = 0 [, resource flags=0[,resourcecontext ]] )
File:必需。規(guī)定要寫入數(shù)據(jù)的文件。如果文件不存在,則創(chuàng)建一個新文件。
Data:必需。規(guī)定要寫入文件的數(shù)據(jù)??梢允亲址?、數(shù)組或數(shù)據(jù)流。
Mode:可選。規(guī)定如何打開/寫入文件??赡艿闹担篎ILE_USE_INCLUDE_PATH/FILE_APPEND/LOCK_EX
Context:可選。規(guī)定文件句柄的環(huán)境。context 是一套可以修改流的行為的選項。
// 寫入文件 echo file_put_contents("sites.txt","Runoob"); echo "<hr>";
9 :filesize()
函數(shù)返回指定文件的大小。
如果成功,該函數(shù)返回文件大小的字節(jié)數(shù)。如果失敗,則返回 FALSE。
語法:filesize(filename)
Filename:必需。規(guī)定要檢查的文件。
// 返回文件大小 echo filesize("target.txt"); echo "<hr>";
10 :filetype()
函數(shù)返回指定文件或目錄的類型。
若成功,則返回 7 種可能的值。若失敗,則返回 false。
語法:filetype(filename)
Filename:必需。規(guī)定要檢查的文件。
// 返回文件類型 echo filetype("target.txt"); echo "<hr>";
11 :glob()
返回一個包含匹配指定模式的文件名/目錄的數(shù)組。
glob() 函數(shù)返回匹配指定模式的文件名或目錄。
該函數(shù)返回一個包含有匹配文件 / 目錄的數(shù)組。如果出錯返回 false。
語法:glob(pattern,flags)
File:必需。規(guī)定檢索模式。
Size:可選。規(guī)定特殊的設(shè)定。
- GLOB_MARK - 在每個返回的項目中加一個斜線
- GLOB_NOSORT - 按照文件在目錄中出現(xiàn)的原始順序返回(不排序)
- GLOB_NOCHECK - 如果沒有文件匹配則返回用于搜索的模式
- GLOB_NOESCAPE - 反斜線不轉(zhuǎn)義元字符
- GLOB_BRACE - 擴充 {a,b,c} 來匹配 'a','b' 或 'c'
- GLOB_ONLYDIR - 僅返回與模式匹配的目錄項
- GLOB_ERR - 停止并讀取錯誤信息(比如說不可讀的目錄),默認(rèn)的情況下忽略所有錯誤
注釋:GLOB_ERR 是 PHP 5.1 添加的。
echo "<pre class="brush:php;toolbar:false">"; var_dump(glob("*.*")); echo "<hr>";
12 :is_dir()
判斷指定的文件名是否是一個目錄。
語法:is_dir(file)
File:必需。規(guī)定要檢查的文件。
$file = "D:/wwwroot/xxx.com/"; if(is_dir($file)) { echo ("$file is a directory"); } else { echo ("$file is not a directory"); } echo "<hr>";
13 :is_writable()
判斷文件是否可寫。如果文件存在并且可寫則返回 true。
語法:is_writable(file)
File:必需。規(guī)定要檢查的文件。
$file = "target.txt"; if(is_writable($file)) { echo ("$file is writeable"); } else { echo ("$file is not writeable"); } echo "<hr>";
14 :mkdir()
創(chuàng)建目錄,如果成功該函數(shù)返回 TRUE,如果失敗則返回 FALSE。
語法:mkdir(path,mode,recursive,context)
Path:必需。規(guī)定要創(chuàng)建的目錄的名稱。
Mode:可選。規(guī)定權(quán)限。默認(rèn)是 0777(允許全局訪問)。
mode 參數(shù)由四個數(shù)字組成:
第一個數(shù)字通常是 0
第二個數(shù)字規(guī)定所有者的權(quán)限
第三個數(shù)字規(guī)定所有者所屬的用戶組的權(quán)限
第四個數(shù)字規(guī)定其他所有人的權(quán)限
可能的值(如需設(shè)置多個權(quán)限,請對下面的數(shù)字進行總計):
1 = 執(zhí)行權(quán)限
2 = 寫權(quán)限
4 = 讀權(quán)限
Recursive:可選。規(guī)定是否設(shè)置遞歸模式。(PHP 5 中新增的)
Context:可選。規(guī)定文件句柄的環(huán)境。context 是一套可以修改流的行為的選項。(PHP 5 中新增的)
echo mkdir("testing"); echo "<hr>";
注釋: mode 參數(shù)在 Windows 平臺上被忽略。
15 :move_uploaded_file()
將上傳的文件移動到新位置。若成功,則返回 true,否則返回 false。
文件上傳的核心就是這個文件
語法:move_uploaded_file(file,newloc)
File:必需。規(guī)定要移動的文件。
Newloc:必需。規(guī)定文件的新位置。
注釋:本函數(shù)僅用于通過 Http POST 上傳的文件。
注意:如果目標(biāo)文件已經(jīng)存在,將會被覆蓋。
16 :parse_ini_file()
函數(shù)解析一個配置文件(ini 文件),并以數(shù)組的形式返回其中的設(shè)置。
語法:parse_ini_file(file,process_sect<strong class="keylink">io</strong>ns)
File:必需。規(guī)定要檢查的 ini 文件。
process_sections:可選。如果設(shè)置為 TRUE,則返回一個多維數(shù)組,包括了配置文件中每一節(jié)的名稱和設(shè)置。默認(rèn)是 FALSE。
echo "<pre class="brush:php;toolbar:false">"; var_dump(parse_ini_file("test.ini")); echo "<hr>";
注:此ini文件不一定非的是php.ini,也可以是你自己的ini配置文件。
17 :realpath()
該函數(shù)刪除所有符號連接(比如 '/./', '/../' 以及多余的 '/'),并返回絕對路徑名。
如果失敗,該函數(shù)返回 FALSE。
語法:realpath(path)
Path:必需。規(guī)定要檢查的路徑。
echo realpath("test.ini");
18 :rename()
rename() 函數(shù)重命名文件或目錄。
如果成功,該函數(shù)返回 TRUE。如果失敗,則返回 FALSE。
語法:rename(oldname,newname,context)
Oldname:必需。規(guī)定要重命名的文件或目錄。
Newname:必需。規(guī)定文件或目錄的新名稱。
Context:可選。規(guī)定文件句柄的環(huán)境。context 是一套可以修改流的行為的選項。
echo rename("test.ini","testss.ini"); echo "<hr>";
19 :tempnam()
創(chuàng)建唯一的臨時文件。若成功,則該函數(shù)返回新的臨時文件名。若失敗,則返回 false。
語法:tempnam(dir,prefix)
Dir:必需。規(guī)定創(chuàng)建臨時文件的目錄。
Prefix:必需。規(guī)定文件名的開頭。
echo tempnam("D:wwwrootxxx.com","TMP0"); echo "<hr>";
注: 此方法創(chuàng)建的文件,如果不再需要該文件則要刪除此文件,不會自動刪除的。
20 :tmpfile()
建立臨時文件。此函數(shù)創(chuàng)建的臨時文件會在文件關(guān)閉后(用 fclose())或當(dāng)腳本結(jié)束后自動被刪除。
語法:tmpfile()
$temp = tmpfile(); fwrite($temp, "Testing, testing."); // 將文件指針的位置倒回文件的開頭。 rewind($temp); // 從文件中讀取1K數(shù)據(jù) echo fread($temp,1024); //This removes the file fclose($temp);
21:unlink()
刪除文件。如果成功,該函數(shù)返回 TRUE。如果失敗,則返回 FALSE。
語法:unlink(filename,context)
Filename:必需。規(guī)定要刪除的文件。
Context:可選。規(guī)定文件句柄的環(huán)境。context 是一套可以修改流的行為的選項。
// 如果沒有text.txt文件,這樣寫輸出的結(jié)果會報警告,測試代碼,就這樣了 // 實際用的時候,需要注意這個問題 $file = "test.txt"; if (!unlink($file)) { echo ("Error deleting $file"); } else { echo ("Deleted $file"); }
22 :chmod()
改變文件權(quán)限。如果成功則返回 TRUE,如果失敗則返回 FALSE。
語法:chmod(file,mode)
File:必需。規(guī)定要檢查的文件。
Mode:必需。規(guī)定新的權(quán)限。
mode 參數(shù)由 4 個數(shù)字組成:
第一個數(shù)字通常是 0
第二個數(shù)字規(guī)定所有者的權(quán)限
第三個數(shù)字規(guī)定所有者所屬的用戶組的權(quán)限
第四個數(shù)字規(guī)定其他所有人的權(quán)限
可能的值(如需設(shè)置多個權(quán)限,請對下面的數(shù)字進行總計):
1 = 執(zhí)行權(quán)限
2 = 寫權(quán)限
4 = 讀權(quán)限
echo chmod("target.txt",0600); echo "<hr>";
23:chown()
改變文件所有者。如果成功則返回 TRUE,如果失敗則返回 FALSE。
語法:chown(file,owner)
File:必需。規(guī)定要檢查的文件。
Owner:必需。規(guī)定新的所有者??梢允怯脩裘蛴脩舻?ID。
echo chown("target.txt","root"); echo "<hr>";
24:chgrp()
改變文件組。如果成功則返回 TRUE,否則返回 FALSE。
語法:chgrp(file,group)
File:必需。規(guī)定要檢查的文件。
Group:可選。規(guī)定新的組。可以是組名或組的 ID。
echo chgrp("test.txt","admin"); echo "<hr>";
The above is the detailed content of Summary of commonly used file operation functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article will explain in detail how PHP formats rows into CSV and writes file pointers. I think it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Format rows to CSV and write to file pointer Step 1: Open file pointer $file=fopen("path/to/file.csv","w"); Step 2: Convert rows to CSV string using fputcsv( ) function converts rows to CSV strings. The function accepts the following parameters: $file: file pointer $fields: CSV fields as an array $delimiter: field delimiter (optional) $enclosure: field quotes (

This article will explain in detail about changing the current umask in PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Overview of PHP changing current umask umask is a php function used to set the default file permissions for newly created files and directories. It accepts one argument, which is an octal number representing the permission to block. For example, to prevent write permission on newly created files, you would use 002. Methods of changing umask There are two ways to change the current umask in PHP: Using the umask() function: The umask() function directly changes the current umask. Its syntax is: intumas

This article will explain in detail about PHP calculating the MD5 hash of files. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP calculates the MD5 hash of a file MD5 (MessageDigest5) is a one-way encryption algorithm that converts messages of arbitrary length into a fixed-length 128-bit hash value. It is widely used to ensure file integrity, verify data authenticity and create digital signatures. Calculating the MD5 hash of a file in PHP PHP provides multiple methods to calculate the MD5 hash of a file: Use the md5_file() function. The md5_file() function directly calculates the MD5 hash value of the file and returns a 32-character

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ??in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ??swapped. $original_array=[

This article will explain in detail the numerical encoding of the error message returned by PHP in the previous Mysql operation. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. . Using PHP to return MySQL error information Numeric Encoding Introduction When processing mysql queries, you may encounter errors. In order to handle these errors effectively, it is crucial to understand the numerical encoding of error messages. This article will guide you to use php to obtain the numerical encoding of Mysql error messages. Method of obtaining the numerical encoding of error information 1. mysqli_errno() The mysqli_errno() function returns the most recent error number of the current MySQL connection. The syntax is as follows: $erro

This article will explain in detail how PHP determines whether a specified key exists in an array. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP determines whether a specified key exists in an array: In PHP, there are many ways to determine whether a specified key exists in an array: 1. Use the isset() function: isset($array["key"]) This function returns a Boolean value, true if the specified key exists, false otherwise. 2. Use array_key_exists() function: array_key_exists("key",$arr

This article will explain in detail about obtaining pi in PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Introduction to Obtaining Pi with PHP Pi (π) is the ratio of the circumference to the diameter of a circle. It is an irrational number and cannot be expressed with a finite number of digits. In php, you can use the built-in function M_PI to get an approximate value of pi. M_PI function The M_PI function returns an approximate value of pi, accurate to 14 decimal places. It is a constant of PHP, so you don't need to use any parameters to use it. Syntax output 3.14159265358979 Alternative methods In addition to the M_PI function, there are some alternatives

This article will explain in detail to you the length of the string that matches the mask for the first time in the string returned by PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Get the length of the first substring in a string that matches the mask in PHP. In PHP, you can use the preg_match() function to get the first substring in a string that matches the given mask and return its length. The syntax is as follows: intpreg_match(string$pattern,string$subject,array&$matches=null,int$flags=0,int$offset=0):in
