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()
函数返回指定文件的大小。
如果成功,该函数返回文件大小的字节数。如果失败,则返回 FALSE。
语法:filesize(filename)
Filename:必需。规定要检查的文件。
// 返回文件大小 echo filesize("target.txt"); echo "<hr>";
10 :filetype()
函数返回指定文件或目录的类型。
若成功,则返回 7 种可能的值。若失败,则返回 false。
语法:filetype(filename)
Filename:必需。规定要检查的文件。
// 返回文件类型 echo filetype("target.txt"); echo "<hr>";
11 :glob()
返回一个包含匹配指定模式的文件名/目录的数组。
glob() 函数返回匹配指定模式的文件名或目录。
该函数返回一个包含有匹配文件 / 目录的数组。如果出错返回 false。
语法:glob(pattern,flags)
File:必需。规定检索模式。
Size:可选。规定特殊的设定。
- GLOB_MARK - 在每个返回的项目中加一个斜线
- GLOB_NOSORT - 按照文件在目录中出现的原始顺序返回(不排序)
- 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:必需。规定要检查的文件。
$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:必需。规定要检查的文件。
$file = "target.txt"; if(is_writable($file)) { echo ("$file is writeable"); } else { echo ("$file is not writeable"); } echo "<hr>";
14 :mkdir()
创建目录,如果成功该函数返回 TRUE,如果失败则返回 FALSE。
语法:mkdir(path,mode,recursive,context)
Path:必需。规定要创建的目录的名称。
Mode:可选。规定权限。默认是 0777(允许全局访问)。
mode 参数由四个数字组成:
第一个数字通常是 0
第二个数字规定所有者的权限
第三个数字规定所有者所属的用户组的权限
第四个数字规定其他所有人的权限
可能的值(如需设置多个权限,请对下面的数字进行总计):
1 = 执行权限
2 = 写权限
4 = 读权限
Recursive:可选。规定是否设置递归模式。(PHP 5 中新增的)
Context:可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。(PHP 5 中新增的)
echo mkdir("testing"); echo "<hr>";
注释: mode 参数在 Windows 平台上被忽略。
15 :move_uploaded_file()
将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
文件上传的核心就是这个文件
语法:move_uploaded_file(file,newloc)
File:必需。规定要移动的文件。
Newloc:必需。规定文件的新位置。
注释:本函数仅用于通过 Http POST 上传的文件。
注意:如果目标文件已经存在,将会被覆盖。
16 :parse_ini_file()
函数解析一个配置文件(ini 文件),并以数组的形式返回其中的设置。
语法:parse_ini_file(file,process_sect<strong class="keylink">io</strong>ns)
File:必需。规定要检查的 ini 文件。
process_sections:可选。如果设置为 TRUE,则返回一个多维数组,包括了配置文件中每一节的名称和设置。默认是 FALSE。
echo "<pre class="brush:php;toolbar:false">"; var_dump(parse_ini_file("test.ini")); echo "<hr>";
注:此ini文件不一定非的是php.ini,也可以是你自己的ini配置文件。
17 :realpath()
该函数删除所有符号连接(比如 '/./', '/../' 以及多余的 '/'),并返回绝对路径名。
如果失败,该函数返回 FALSE。
语法:realpath(path)
Path:必需。规定要检查的路径。
echo realpath("test.ini");
18 :rename()
rename() 函数重命名文件或目录。
如果成功,该函数返回 TRUE。如果失败,则返回 FALSE。
语法:rename(oldname,newname,context)
Oldname:必需。规定要重命名的文件或目录。
Newname:必需。规定文件或目录的新名称。
Context:可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。
echo rename("test.ini","testss.ini"); echo "<hr>";
19 :tempnam()
创建唯一的临时文件。若成功,则该函数返回新的临时文件名。若失败,则返回 false。
语法:tempnam(dir,prefix)
Dir:必需。规定创建临时文件的目录。
Prefix:必需。规定文件名的开头。
echo tempnam("D:wwwrootxxx.com","TMP0"); echo "<hr>";
注: 此方法创建的文件,如果不再需要该文件则要删除此文件,不会自动删除的。
20 :tmpfile()
建立临时文件。此函数创建的临时文件会在文件关闭后(用 fclose())或当脚本结束后自动被删除。
语法:tmpfile()
$temp = tmpfile(); fwrite($temp, "Testing, testing."); // 将文件指针的位置倒回文件的开头。 rewind($temp); // 从文件中读取1K数据 echo fread($temp,1024); //This removes the file fclose($temp);
21:unlink()
删除文件。如果成功,该函数返回 TRUE。如果失败,则返回 FALSE。
语法:unlink(filename,context)
Filename:必需。规定要删除的文件。
Context:可选。规定文件句柄的环境。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:必需。规定要检查的文件。
Mode:必需。规定新的权限。
mode 参数由 4 个数字组成:
第一个数字通常是 0
第二个数字规定所有者的权限
第三个数字规定所有者所属的用户组的权限
第四个数字规定其他所有人的权限
可能的值(如需设置多个权限,请对下面的数字进行总计):
1 = 执行权限
2 = 写权限
4 = 读权限
echo chmod("target.txt",0600); echo "<hr>";
23:chown()
改变文件所有者。如果成功则返回 TRUE,如果失败则返回 FALSE。
语法:chown(file,owner)
File:必需。规定要检查的文件。
Owner:必需。规定新的所有者。可以是用户名或用户的 ID。
echo chown("target.txt","root"); echo "<hr>";
24:chgrp()
改变文件组。如果成功则返回 TRUE,否则返回 FALSE。
语法:chgrp(file,group)
File:必需。规定要检查的文件。
Group:可选。规定新的组。可以是组名或组的 ID。
echo chgrp("test.txt","admin"); echo "<hr>";
以上是PHP常用的檔案操作函數總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

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

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

這篇文章將為大家詳細講解有關PHP建立一個具有唯一文件名的文件,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章後可以有所收穫。在PHP中建立唯一檔案名稱的檔案簡介在php中建立具有唯一檔案名稱的檔案對於組織和管理檔案系統至關重要。唯一文件名稱可確保不會覆蓋現有文件,並便於尋找和檢索特定文件。本指南將介紹在PHP中產生唯一檔案名稱的幾種方法。方法1:使用uniqid()函數uniqid()函數產生一個基於當前時間和微秒的唯一字串。此字串可以作為檔案名稱的基礎。

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

這篇文章將為大家詳細講解有關PHP將文件截斷到給定的長度,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP檔案截斷簡介php中的file_put_contents()函數可用來將檔案截斷到指定長度。截斷是指刪除檔案末端的部分內容,從而縮短檔案長度。語法file_put_contents($filename,$data,SEEK_SET,$offset);$filename:要截斷的檔案路徑。 $data:要寫入檔案的空字串。 SEEK_SET:指定為檔案開始處

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

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

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