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"; // 輸出文件名,包含擴展名 echo basename($path) ."<br/>";// home.php // 輸出文件名,不包含擴展名 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.
// 復制文件,返回值為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()
返回一個包含匹配指定模式的文件名/目錄的數(shù)組。
glob() 函數(shù)返回匹配指定模式的文件名或目錄。
該函數(shù)返回一個包含有匹配文件 / 目錄的數(shù)組。如果出錯返回 false。
語法:glob(pattern,flags)
File:必需。規(guī)定檢索模式。
Size:可選。規(guī)定特殊的設定。
- GLOB_MARK - 在每個返回的項目中加一個斜線
- GLOB_NOSORT - 按照文件在目錄中出現(xiàn)的原始順序返回(不排序)
- GLOB_NOCHECK - 如果沒有文件匹配則返回用于搜索的模式
- GLOB_NOESCAPE - 反斜線不轉義元字符
- GLOB_BRACE - 擴充 {a,b,c} 來匹配 'a','b' 或 'c'
- GLOB_ONLYDIR - 僅返回與模式匹配的目錄項
- GLOB_ERR - 停止并讀取錯誤信息(比如說不可讀的目錄),默認的情況下忽略所有錯誤
注釋: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ī)定權限。默認是 0777(允許全局訪問)。
mode 參數(shù)由四個數(shù)字組成:
第一個數(shù)字通常是 0
第二個數(shù)字規(guī)定所有者的權限
第三個數(shù)字規(guī)定所有者所屬的用戶組的權限
第四個數(shù)字規(guī)定其他所有人的權限
可能的值(如需設置多個權限,請對下面的數(shù)字進行總計):
1 = 執(zhí)行權限
2 = 寫權限
4 = 讀權限
Recursive:可選。規(guī)定是否設置遞歸模式。(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 上傳的文件。
注意:如果目標文件已經(jīng)存在,將會被覆蓋。
16 :parse_ini_file()
函數(shù)解析一個配置文件(ini 文件),并以數(shù)組的形式返回其中的設置。
語法:parse_ini_file(file,process_sect<strong class="keylink">io</strong>ns)
File:必需。規(guī)定要檢查的 ini 文件。
process_sections:可選。如果設置為 TRUE,則返回一個多維數(shù)組,包括了配置文件中每一節(jié)的名稱和設置。默認是 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)建的臨時文件會在文件關閉后(用 fclose())或當腳本結束后自動被刪除。
語法: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文件,這樣寫輸出的結果會報警告,測試代碼,就這樣了 // 實際用的時候,需要注意這個問題 $file = "test.txt"; if (!unlink($file)) { echo ("Error deleting $file"); } else { echo ("Deleted $file"); }
22 :chmod()
改變文件權限。如果成功則返回 TRUE,如果失敗則返回 FALSE。
語法:chmod(file,mode)
File:必需。規(guī)定要檢查的文件。
Mode:必需。規(guī)定新的權限。
mode 參數(shù)由 4 個數(shù)字組成:
第一個數(shù)字通常是 0
第二個數(shù)字規(guī)定所有者的權限
第三個數(shù)字規(guī)定所有者所屬的用戶組的權限
第四個數(shù)字規(guī)定其他所有人的權限
可能的值(如需設置多個權限,請對下面的數(shù)字進行總計):
1 = 執(zhí)行權限
2 = 寫權限
4 = 讀權限
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ī)定新的組??梢允墙M名或組的 ID。
echo chgrp("test.txt","admin"); echo "<hr>";
以上是PHP常用的檔案操作函數(shù)總結的詳細內(nèi)容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

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

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

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

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

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

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

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

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

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

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

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

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

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

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