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è)萘?。返回字?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 - 停止并讀取錯誤信息(比如說不可讀的目錄),默認的情況下忽略所有錯誤
注釋: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)限。默認是 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è)置。默認是 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ī)定新的組??梢允墙M名或組的 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.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

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)

Go and Node.js have differences in typing (strong/weak), concurrency (goroutine/event loop), and garbage collection (automatic/manual). Go has high throughput and low latency, and is suitable for high-load backends; Node.js is good at asynchronous I/O and is suitable for high concurrency and short requests. Practical cases of the two include Kubernetes (Go), database connection (Node.js), and web applications (Go/Node.js). The final choice depends on application needs, team skills, and personal preference.

Yes,ReactapplicationscanbeSEO-friendlywithproperstrategies.1)Useserver-siderendering(SSR)withtoolslikeNext.jstogeneratefullHTMLforindexing.2)Implementstaticsitegeneration(SSG)forcontent-heavysitestopre-renderpagesatbuildtime.3)Ensureuniquetitlesandme

目錄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:

Laravel's advantages in back-end development include: 1) elegant syntax and EloquentORM simplify the development process; 2) rich ecosystem and active community support; 3) improved development efficiency and code quality. Laravel's design allows developers to develop more efficiently and improve code quality through its powerful features and tools.

Laravel's core functions in back-end development include routing system, EloquentORM, migration function, cache system and queue system. 1. The routing system simplifies URL mapping and improves code organization and maintenance. 2.EloquentORM provides object-oriented data operations to improve development efficiency. 3. The migration function manages the database structure through version control to ensure consistency. 4. The cache system reduces database queries and improves response speed. 5. The queue system effectively processes large-scale data, avoid blocking user requests, and improve overall performance.

Semantic HTML tags help search engines better understand and index content by improving web page structure and readability, thereby improving SEO. 1) Use labels with clear meaning (such as, , etc.) structured content to help search engines identify page roles and importance. 2) Improve index efficiency and keyword relevance and improve ranking. 3) Combining content quality and user experience, a comprehensive optimization strategy is formed.

Table of Contents What is DI/Dependency Injection Reasons for Dependency Injection Simple Dependency Injection High-Order Dependency Injection Application of Dependency Injection High-Order Optimization What is DI/Dependency Injection Dependency Injection DI actually refers to the dependence on classes through construction In layman's terms, it means that you are currently operating a class, but some methods or functions of this class cannot be completed by this class alone, but are the most direct sign that it can be completed with the help of another class. That is when the parameter data is passed as an object. Strictly speaking, you want to operate one class in another class. There is an interdependence between the two classes. The method of passing parameters is called injection. The reason why dependency injection occurs is that when dependency injection is not used, PHP needs to be in a When a class uses another class

We will learn to create an authentication system using Laravel's PassportapiOAuth. Step 1. Install Laravel We need to use the following command to create a latest Laravel application, so please open the terminal and execute: laravelnewauth Step 2. Install the LaravelPassport package LaravelPassport can implement a complete OAuth2 server for your application in a few minutes. composerrequirelaravel/passport Step 3. Run database migration Passport migration will create your application to store the client and AccessToke
