PHP 檔案處理

王林
發布: 2024-08-29 13:06:58
原創
542 人瀏覽過

所有現代軟體都需要與文件互動。它們要么需要接受文件形式的輸入,要么生成輸出並將其添加到文件中。無論哪種情況,與文件整合的功能都已成為幾乎所有用於運營業務的軟體的不可或缺的功能。對於任何應用程式來說,文件的處理都是必要的。必須處理該文件才能執行某些任務。 PHP 中的檔案處理類似於任何語言(例如 C)的檔案處理。 PHP 有許多普通的檔案函數可以使用。

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

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

PHP 檔案處理能力的用例

例如,銀行需要軟體來幫助他們產生3 個月或6 個月期間的銀行帳戶對帳單等報告,電子商務公司需要列印與庫存和銷售相關的報告,最後但並非最不重要的一點與股票市場交易相關的應用程式需要以可讀檔案的形式提供每日股票價格。我確信透過這個例子,您會同意任何支援業務功能的軟體都需要您讀取資料或將資料寫入檔案。

由於檔案處理能力在現代應用程式中幾乎是必需的,因此所有著名的程式語言(例如Python、Java、C# 和PHP)都提供了內建的檔案處理功能,開發人員可以利用這些功能來開發互動式應用程式。

PHP 中的檔案處理功能

PHP 支援下列檔案格式的讀寫操作。

  • 文字檔: 副檔名為 .txt 的檔案
  • 日誌檔: 副檔名為 .log 的檔案
  • 自訂副檔名: 具有自訂副檔名(如 .abc)的檔案
  • CSV 檔案: 副檔名為 .csv 的檔案
  • 圖片檔: 副檔名為 .jpg/png/gif 的檔案
  • 具有初始化設定的檔案: 副檔名為 .ini 的檔案

PHP 中的檔案處理函數

PHP 提供了廣泛的內建函數來執行各種文件操作。這些檔案函數適用於所有作業系統,例如 Linus、Unix、MAC 和 Windows。但是,MAC OS 和 Windows 中的檔案名稱不區分大小寫,而 Unix 和 Linux 中的檔案名稱區分大小寫。因此,為了避免任何混淆或錯誤,最好的做法是用小寫字母命名所有文件,因為它可以確保完整的平台相容性。

現在我們已經對 php 檔案處理函數的工作原理有了較高的了解,讓我們一一了解這些函數。

1. file_exists() 函數

此函數用於驗證作為參數提供給它的檔案名稱是否存在。它用於避免由於嘗試讀取或寫入不存在的檔案而可能導致的錯誤。

文法:

<?php
file_exists($file_name) //where file_name  would be a file with one of the supported extensions
?>
登入後複製

如果檔案存在,file_exists() 將傳回 True 值,如果檔案不存在,則傳回 false。

現在讓我們在程式碼規格中使用此函數來檢查檔案是否存在。讓我們在根資料夾中放​​置一個名為“mysettings.ini”的文件,並嘗試使用以下程式碼存取它。

代碼:

<?php
if (file_exists('mysettings.ini))
{
echo 'yay! file found!';
}
else
{
echo 'Sorry! mysettings.ini does not exist';
}
?>
登入後複製

輸出:

PHP 檔案處理

現在,如果我們從該位置刪除檔案並運行上面的程式碼,我們將看到以下輸出。

PHP 檔案處理

2. fopen() 函數

fopen 函數在 php 中用來開啟要由應用程式讀取的檔案。

文法:

<?php
fopen($fname,$mode,$use_include_path,$context);
?>
登入後複製

在上面的語法中,$fname代表檔案名,$mode代表我們要開啟檔案的模式。 $mode 可以是下列值之一。

  • r: For opening the file in only read-only mode. It returns false if the file name supplied is not found on the location supplied.
  • r+: For opening the file in both read and write mode. Similar to ‘r’, it also returns false if a file is not found.
  • w: For opening the file in only write-only mode. If the file supplied doesn’t exist, it attempts to create one.
  • w+: For opening the file in both read and write mode. Similar to ‘w’, it also attempts to create a file if the file name supplied is not found.
  • a: For opening the file in write-only mode and appending to the end of the file. If the file supplied doesn’t exist, it attempts to create one.
  • a+: For opening the file in both read and write mode. Similar to ‘a’, it also attempts to create a file if the file name supplied is not found.

3. fwrite() Function

As the name suggests, this function is used to write content to files.

Syntax:

<?php
fwrite($handle, $data_string, $len);
?>
登入後複製

Where $handle is the file location, $data_string is the text string we would like to write to the file and $len is the optional parameter to specify the maximum length of the file.

4. fclose() Function

The fclose() function is used in php when the read/write operations on file are completed and we would like to close the file.

Syntax:

<?php
fclose($file_handle);
?>
登入後複製

Where $file_handle stands for the file pointer.

5. fgets() Function

The fgets() function is used in php to read the file line by line.

Syntax:

<?php
fgets($file_handle);
?>
登入後複製

Where $file_handle stands for the file pointer.

6. copy() Function

The copy() function allows us to copy a file in php.

Syntax:

<?php
copy($file1, $file2);
?>
登入後複製

Where $file1 is the original file and $file2 is the copied file location.

7. unlink() Function

The unlink() function in Php is used to delete a file.

Syntax:

<?php
unlink($filename);
?>
登入後複製

Where the $filename is the filename to be deleted.

Conclusion

With the above example, we can easily conclude that php has a wide variety of in-built functions that simplify reading and writing operations on the file. The most commonly used function include fopen() to open the file in different modes, fwrite() to write data to the file, fread() to read the file content, fclose() to close the file once the necessary operation is done, copy() to copy one file content to another and unlink to delete the unwanted files.

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

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!