Directory
- 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 Editor Youzi carefully organized PHP This summary of commonly used file operation functions provides a practical reference guide for file operation functions for PHP developers. Through the summary of this article, readers can quickly understand various common functions of file operations in PHP and improve the efficiency and accuracy of file processing. Whether you are a beginner or a developer with certain development experience, you can gain practical technical knowledge and experience from the article.
Note: The behavior of file operation functions is affected by the settings in php.ini.
When specifying paths on Unix platforms, the forward slash (/) is used as the directory separator. On the windows platform, both forward slash (/) and backslash () can be used.
1: basename()
Return the file name in the path. Divided into with extension and without extension.
Syntax: basename(path,suffix)
Path: required. Specifies the path to be checked.
Suffix: Optional. Specifies the file extension. If the file name has a file extension, this extension will not be displayed.
// basename $path = "/testWEB/home.php"; // 輸出文件名,包含擴(kuò)展名 echo basename($path) ."<br/>";// home.php // 輸出文件名,不包含擴(kuò)展名 echo basename($path,".php");// home
2: copy()
Copy files. This function returns TRUE if successful and FALSE if failed. If the target file already exists, it will be overwritten.
Syntax: copy(file,to_file)
File: required. Specifies the files to be copied.
to_file: required. Specifies the destination for copied files.
// 復(fù)制文件,返回值為bool echo copy("source.txt","target.txt");
3: dirname()
Return the directory part of the path.
Syntax: dirname(path)
Path: required. Specifies the path to be checked.
// 返回文件路徑 echo dirname("c:/testweb/home.php")."<br>";// c:/testweb/ echo dirname("/testweb/home.php");// /testweb/
4: disk_free_space()
Return the available space of the directory. , in bytes.
Syntax: disk_free_space(directory)
Directory: required. Specifies the directories to be checked. (This directory has restrictions)
// 返回指定目錄的可用空間(查詢的目錄是有限制的) echo disk_free_space("D:/wwwroot/xxx.com/");
5: disk_total_space()
Return the total disk capacity of a directory. Return number of bytes
Syntax: disk_total_space(directory)
Directory: required. Specifies the directories to be checked.
echo disk_total_space("C:/Windows/Temp/"); echo "<hr>";
6: file_exists()
Check whether the file or directory exists. Return bool value
Syntax: file_exists(path)
Path: required. Specifies the path to be checked.
// 查看test.txt是否存在,返回bool值 echo file_exists("target.txt"); // 1 echo "<hr>";
7: file_get_contents()
Read the file into a string .
Syntax: file_get_contents(path,include_path,context,start,max_length)
Path: required. Specifies the file to be read.
include_path: optional. Set this parameter to '1' if you also want to search for files in include_path (in php.ini).
Context: Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream. If NULL is used, it is ignored.
Start: Optional. Specifies the position in the file to begin reading. This parameter is new in PHP 5.1.
max_length: optional. Specifies the number of bytes to read. This parameter is new in PHP 5.1.
// 讀取文件 echo file_get_contents("target.txt"); echo "<hr>";
Tip: This function is binary safe. (Meaning that both binary data (such as images) and character data can be written using this function.)
8: file_put_contents()
Write string to file. If successful, the function returns the number of characters written to the file. On failure, False is returned.
Syntax: int file_put_contents ( string filename,mixedfilename , mixed filename,mixeddata [, int flags=0[,resourceflags = 0 [, resource flags=0[,resourcecontext ]] )
File: required. Specifies the file to which data is to be written. If the file does not exist, a new file is created.
Data: required. Specifies the data to be written to the file. Can be a string, array or data stream.
Mode: Optional. Specifies how to open/write the file. Possible values: FILE_USE_INCLUDE_PATH/FILE_APPEND/LOCK_EX
Context: Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream.
// 寫入文件 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()
返回一個(gè)包含匹配指定模式的文件名/目錄的數(shù)組。
glob() 函數(shù)返回匹配指定模式的文件名或目錄。
該函數(shù)返回一個(gè)包含有匹配文件 / 目錄的數(shù)組。如果出錯(cuò)返回 false。
語法:glob(pattern,flags)
File:必需。規(guī)定檢索模式。
Size:可選。規(guī)定特殊的設(shè)定。
- GLOB_MARK - 在每個(gè)返回的項(xiàng)目中加一個(gè)斜線
- GLOB_NOSORT - 按照文件在目錄中出現(xiàn)的原始順序返回(不排序)
- GLOB_NOCHECK - 如果沒有文件匹配則返回用于搜索的模式
- GLOB_NOESCAPE - 反斜線不轉(zhuǎn)義元字符
- GLOB_BRACE - 擴(kuò)充 {a,b,c} 來匹配 'a','b' 或 'c'
- GLOB_ONLYDIR - 僅返回與模式匹配的目錄項(xiàng)
- GLOB_ERR - 停止并讀取錯(cuò)誤信息(比如說不可讀的目錄),默認(rèn)的情況下忽略所有錯(cuò)誤
注釋:GLOB_ERR 是 PHP 5.1 添加的。
echo "<pre class="brush:php;toolbar:false">"; var_dump(glob("*.*")); echo "<hr>";
12 :is_dir()
判斷指定的文件名是否是一個(gè)目錄。
語法: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ī)定要?jiǎng)?chuàng)建的目錄的名稱。
Mode:可選。規(guī)定權(quán)限。默認(rèn)是 0777(允許全局訪問)。
mode 參數(shù)由四個(gè)數(shù)字組成:
第一個(gè)數(shù)字通常是 0
第二個(gè)數(shù)字規(guī)定所有者的權(quán)限
第三個(gè)數(shù)字規(guī)定所有者所屬的用戶組的權(quán)限
第四個(gè)數(shù)字規(guī)定其他所有人的權(quán)限
可能的值(如需設(shè)置多個(gè)權(quán)限,請(qǐng)對(duì)下面的數(shù)字進(jìn)行總計(jì)):
1 = 執(zhí)行權(quán)限
2 = 寫權(quán)限
4 = 讀權(quán)限
Recursive:可選。規(guī)定是否設(shè)置遞歸模式。(PHP 5 中新增的)
Context:可選。規(guī)定文件句柄的環(huán)境。context 是一套可以修改流的行為的選項(xiàng)。(PHP 5 中新增的)
echo mkdir("testing"); echo "<hr>";
注釋: mode 參數(shù)在 Windows 平臺(tái)上被忽略。
15 :move_uploaded_file()
將上傳的文件移動(dòng)到新位置。若成功,則返回 true,否則返回 false。
文件上傳的核心就是這個(gè)文件
語法:move_uploaded_file(file,newloc)
File:必需。規(guī)定要移動(dòng)的文件。
Newloc:必需。規(guī)定文件的新位置。
注釋:本函數(shù)僅用于通過 Http POST 上傳的文件。
注意:如果目標(biāo)文件已經(jīng)存在,將會(huì)被覆蓋。
16 :parse_ini_file()
函數(shù)解析一個(gè)配置文件(ini 文件),并以數(shù)組的形式返回其中的設(shè)置。
語法:parse_ini_file(file,process_sect<strong class="keylink">io</strong>ns)
File:必需。規(guī)定要檢查的 ini 文件。
process_sections:可選。如果設(shè)置為 TRUE,則返回一個(gè)多維數(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ù)刪除所有符號(hào)連接(比如 '/./', '/../' 以及多余的 '/'),并返回絕對(duì)路徑名。
如果失敗,該函數(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 是一套可以修改流的行為的選項(xiàng)。
echo rename("test.ini","testss.ini"); echo "<hr>";
19 :tempnam()
創(chuàng)建唯一的臨時(shí)文件。若成功,則該函數(shù)返回新的臨時(shí)文件名。若失敗,則返回 false。
語法:tempnam(dir,prefix)
Dir:必需。規(guī)定創(chuàng)建臨時(shí)文件的目錄。
Prefix:必需。規(guī)定文件名的開頭。
echo tempnam("D:wwwrootxxx.com","TMP0"); echo "<hr>";
注: 此方法創(chuàng)建的文件,如果不再需要該文件則要?jiǎng)h除此文件,不會(huì)自動(dòng)刪除的。
20 :tmpfile()
建立臨時(shí)文件。此函數(shù)創(chuàng)建的臨時(shí)文件會(huì)在文件關(guān)閉后(用 fclose())或當(dāng)腳本結(jié)束后自動(dòng)被刪除。
語法: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ī)定要?jiǎng)h除的文件。
Context:可選。規(guī)定文件句柄的環(huán)境。context 是一套可以修改流的行為的選項(xiàng)。
// 如果沒有text.txt文件,這樣寫輸出的結(jié)果會(huì)報(bào)警告,測(cè)試代碼,就這樣了 // 實(shí)際用的時(shí)候,需要注意這個(gè)問題 $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 個(gè)數(shù)字組成:
第一個(gè)數(shù)字通常是 0
第二個(gè)數(shù)字規(guī)定所有者的權(quán)限
第三個(gè)數(shù)字規(guī)定所有者所屬的用戶組的權(quán)限
第四個(gè)數(shù)字規(guī)定其他所有人的權(quán)限
可能的值(如需設(shè)置多個(gè)權(quán)限,請(qǐng)對(duì)下面的數(shù)字進(jìn)行總計(jì)):
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>";
以上是PHP常用的檔案操作函數(shù)總結(jié)的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)

這篇文章將為大家詳細(xì)講解有關(guān)PHP將行格式化為CSV並寫入文件指針,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章後可以有所收穫。將行格式化為CSV並寫入檔案指標(biāo)步驟1:開啟檔案指標(biāo)$file=fopen("path/to/file.csv","w");步驟2:將行轉(zhuǎn)換為CSV字串使用fputcsv( )函數(shù)將行轉(zhuǎn)換為CSV字串。此函數(shù)接受以下參數(shù):$file:檔案指標(biāo)$fields:作為陣列的CSV欄位$delimiter:欄位分隔符號(hào)(可選)$enclosure:欄位引號(hào)(

這篇文章將為大家詳細(xì)講解有關(guān)PHP改變當(dāng)前的umask,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP更改目前的umask概述umask是一個(gè)用於設(shè)定新建立的檔案和目錄的預(yù)設(shè)檔案權(quán)限的php函數(shù)。它接受一個(gè)參數(shù),這是一個(gè)八進(jìn)制數(shù)字,表示要阻止的權(quán)限。例如,要阻止對(duì)新建立的檔案進(jìn)行寫入權(quán)限,可以使用002。更改umask的方法有兩種方法可以更改PHP中的目前umask:使用umask()函數(shù):umask()函數(shù)直接變更目前umask。其語法為:intumas

這篇文章將為大家詳細(xì)講解有關(guān)PHP計(jì)算文件的MD5散列,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP計(jì)算檔案的MD5雜湊MD5(MessageDigest5)是一種單向加密演算法,可將任意長(zhǎng)度的訊息轉(zhuǎn)換為固定長(zhǎng)度的128位元雜湊值。它廣泛用於確保文件完整性、驗(yàn)證資料真實(shí)性和建立數(shù)位簽章。在PHP中計(jì)算檔案的MD5雜湊php提供了多種方法來計(jì)算檔案的MD5雜湊:使用md5_file()函數(shù)md5_file()函數(shù)直接計(jì)算檔案的MD5雜湊值,傳回一個(gè)32個(gè)字元的

這篇文章將為大家詳細(xì)講解有關(guān)PHP返回一個(gè)鍵值翻轉(zhuǎn)後的數(shù)組,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP鍵值翻轉(zhuǎn)數(shù)組鍵值翻轉(zhuǎn)是一種對(duì)數(shù)組進(jìn)行的操作,它將數(shù)組中的鍵和值進(jìn)行交換,產(chǎn)生一個(gè)新的數(shù)組,其中原始鍵作為值,原始值作為鍵。實(shí)作方法在php中,可以透過以下方法對(duì)陣列進(jìn)行鍵值翻轉(zhuǎn):array_flip()函數(shù):array_flip()函數(shù)專門用於鍵值翻轉(zhuǎn)操作。它接收一個(gè)數(shù)組作為參數(shù),並傳回一個(gè)新的數(shù)組,其中鍵和值已交換。 $original_array=[

這篇文章將為大家詳細(xì)講解有關(guān)PHP返回上一個(gè)Mysql操作中的錯(cuò)誤訊息的數(shù)字編碼,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章後可以有所收穫。利用PHP回傳MySQL錯(cuò)誤訊息數(shù)字編碼引言在處理mysql查詢時(shí),可能會(huì)遇到錯(cuò)誤。為了有效處理這些錯(cuò)誤,了解錯(cuò)誤訊息數(shù)字編碼至關(guān)重要。本文將指導(dǎo)您使用php取得Mysql錯(cuò)誤訊息數(shù)字編碼。取得錯(cuò)誤訊息數(shù)字編碼的方法1.mysqli_errno()mysqli_errno()函數(shù)傳回目前MySQL連線的最近錯(cuò)誤號(hào)碼。文法如下:$erro

這篇文章將為大家詳細(xì)講解有關(guān)PHP判斷某個(gè)數(shù)組中是否存在指定的key,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP判斷某個(gè)陣列中是否存在指定的key:在php中,判斷某個(gè)陣列中是否存在指定的key的方法有多種:1.使用isset()函數(shù):isset($array["key"])此函數(shù)傳回布林值,如果指定的key存在,則傳回true,否則傳回false。 2.使用array_key_exists()函數(shù):array_key_exists("key",$arr

這篇文章將為大家詳細(xì)講解有關(guān)PHP獲得圓周率,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP取得圓周率簡(jiǎn)介圓周率(π)是圓的周長(zhǎng)與直徑之比,是一個(gè)無理數(shù),無法用有限位數(shù)表示。在php中,可以使用內(nèi)建函數(shù)M_PI來取得圓周率的近似值。 M_PI函數(shù)M_PI函數(shù)傳回圓周率的近似值,精確到小數(shù)點(diǎn)後14位。它是PHP的常數(shù),因此無需使用任何參數(shù)即可使用它。語法輸出3.14159265358979替代方法除了M_PI函數(shù),還有一些替代方

這篇文章將為大家詳細(xì)講解有關(guān)PHP返回字符串中首次符合mask的字符串長(zhǎng)度,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP中取得字串中首次符合遮罩的子字串長(zhǎng)度在php中,可以使用preg_match()函數(shù)來取得字串中首次符合給定遮罩的子字串,並傳回其長(zhǎng)度。語法如下:intpreg_match(string$pattern,string$subject,array&$matches=null,int$flags=0,int$offset=0):in
