首頁  >  文章  >  後端開發  >  PHP 檔案函數

PHP 檔案函數

王林
王林原創
2024-08-29 13:02:25833瀏覽

PHP 檔案函數是在 PHP 大量內建函數的幫助下處理檔案的最佳且便捷的方式。 Windows 作業系統和 MAC 作業系統不區分大小寫。採用小寫字母命名轉換進行檔案命名是保證最大跨平台相容性的最佳實踐。有一些 PHP 檔案函數對於處理檔案資訊中存在的資料非常有幫助。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

PHP 檔案函數

PHP 檔案函數可協助儲存/刪除/操作/複製檔案中的資料或刪除檔案等。以下是一些文件函數的列表。他們是:

  1. file_exists 函數
  2. fopen 函數
  3. fwrite 函數
  4. fclose 函數
  5. fgets 函數
  6. 複製功能
  7. file_get_contents 函數和
  8. 刪除檔案

PHP檔案功能實作範例

以下是 PHP 檔案函數的範例:

1. PHP file_exists 函數

為了在文件中寫入內容或在刪除中操作數據,首先,您必須檢查該文件是否存在於目錄中才能處理它。如果伺服器中不存在您搜尋的文件並且您想在伺服器上建立新文件,此 PHP 函數還可以幫助您建立新文件。

文法:

<?php
file_exists($file_name);
?>

說明:

「file_exists()」函數是一個 PHP 函數,只有當檔案存在於伺服器中時,它才會傳回結果為 TRUE,如果檔案不存在/在伺服器/伺服器目錄中不存在,結果將傳回 FALSE。 $file_name 變數是檔案路徑以及要檢查的路徑末端的檔案名稱。

範例:

下面的範例使用 file_exists() 函數來決定檔案是否存在。將以下程式碼儲存在語法中的 file_function.php 中,並在瀏覽器中開啟檔案路徑,以便您看到結果/輸出。 File_name.txt 未創建,因此輸出將是 FALSE 的結果,ELSE 條件語句的輸出將是結果。

代碼:

<?php
If(file_exists('file_name.txt'))
{
echo "Now the File Found!!!";
}
else{
echo "your file_name.txt doesnot exist until now";
}
?>

輸出:

PHP 檔案函數

2. PHP fopen 函數

PHP fopen 函數將幫助您開啟伺服器中的檔案。

文法:

<?php
fopen($file_name, $mode, $use_include_path,$context);
?>

說明:

  • 「fopen」是 PHP 檔案函數,用來開啟 server/server 目錄中的檔案。
  • 「$file_name」是實際要開啟的檔案名稱
  • 「模式」就像你想要對檔案執行的操作,如讀取、寫入、追加等
  • 模式「r」將從頭開始讀取文件,如果文件不存在則傳回 false。它有助於只讀而不是讀寫模式。對於讀寫模式,必須使用“r+”模式。
  • 模式「w」將有助於將一些資料寫入檔案。它將把文件截斷為零長度。如果該檔案甚至不存在,則該檔案將被建立為僅寫入而不是讀取和寫入。為了讀寫,將使用“w+”模式。
  • 模式「a」會將檔案附加到結尾。如果該文件甚至不存在,則將以只寫模式建立該文件。對於附加的讀寫模式,將使用“a+”模式。
  • 「$use_include_path」是可選術語,預設結果為 false,如果將其設為 TRUE 結果,則函數也會幫助存在的包含路徑。同樣,“$context”也是可選的,可用於指定上下文支援。

範例:

下面的語法只是開啟名為 file_name.txt 的文件,如果沒有找到,則會列印 die() 函數中的文件,並且當錯誤發生時,會執行 die() 函數。 Die() 將顯示括號內存在的訊息。因此,如果檔案確實存在,則瀏覽器中大多不會有輸出。

代碼:

<?php
$op = fopen("file_name.txt",'w');
or
die("Now we are failed in creating the file");
?>

3. PHP 寫函數

PHP寫入功能將幫助您寫入檔案。

文法:

<?php
fwrite($handle,$string,$length);
?>

Explanation:

  • “fwrite” PHP function will help to write some data to the files.
  • “$handle” term is the file pointer’s resource.
  • “$string” term is the data/information which is to be written inside the file.
  • “$length” term is optional which helps to specify the maximum file length.

4. PHP Fclose Function

Fclose Function will help to close the file which is opened already in the server.

Syntax:

<?php
fclose($handle);
?>

Explanation:

  • “fclose” will helps you to close the function which is opened already in the server/server directory.
  • “$handle” is the pointer’s resource of the file.

5. PHP fgets Function

PHP Fgets Functions will help to read the file/files are red line by line using the syntax:

fgets($handle);
  • “$fgets” is to read the lines of the file.
  • “$handle” is the resource of the file pointer.

Code:

<?php
$op = fopen("file_name.txt",'r');
or
die("Now we are failed in opening the file");
$line1 = fgets(#op);
echo $line1;
fclose($op);
?>

6. PHP Copy Function

PHP copy function will be used in order to copy the files.

Syntax:

copy($file, $file_copied);

Explanation:

  • “$file” is the path of the file which is to be copied.
  • “$file_copied” term is the name of the copied file.

Code:

<?php
copy('file_name.txt','my_backup_settings.txt')
or
die("We can't cop the file");
echo "File now successfully copied to the 'my_backup_settings.txt'";
?>

7. PHP file_get_contents Function

This function helps in reading the entire contents of the file. Difference between the fgets and file_get_contents will return the whole data as a string but the fgets will be red the whole file line by line.

Code:

<?php
echo "<pre class="brush:php;toolbar:false">"; // Enables the display of the line feeds
echo file_get_contents("file_name.txt");
echo "
"; // Now it Terminates the pre tag ?>

8. Deleting a File (Unlink Function)

Unlink Function will help to delete a file.

Code:

<?php
if(!unlink('my_backup_settings.txt'))
{
echo " Cannot delete the file";
}
else
{
echo "file 'my_backup_settings.txt' now deleted successfully";
}
?>

All PHP File Functions help in supporting the wide range of some of the file formats. They are:

  • File_name.txt
  • File_name.log
  • File_name.custom_extension i.e., file_name.xyz
  • File_name.csv
  • File_name.gif, File_name.jpg, etc.
  • Files/File provides permanent cost-effective data storage solution/solutions for the simple data when compared to the databases which require some software and some skills in order to manage the Database Management Systems(DBMS Systems).
  • File Functions helps to store some simple data like the server logs in order to analyze the data or for retrieving the data for future purpose.
  • PHP file functions will help you to store the program/program settings which are like program.ini etc.

Recommended Article

This is a guide to PHP file Functions. Here we discuss the Introduction to PHP file Functions examples along with code implementation and output. You can also go through our other suggested articles to learn more –

  1. Factorial in PHP
  2. PHP Pagination
  3. PHP unset()
  4. PHP MD5()

以上是PHP 檔案函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP加密下一篇:PHP加密