搜尋

PHP readfile 基本上是 PHP 庫中的內建函數,用於讀取檔案然後將其寫入輸出緩衝區。

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

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

文法:

readfile ( string $file_name [, boolean $path = FALSE [, resource $context ]] ) : int

語法中使用的參數:

  • 檔案名稱:這是必填字段,我們在其中提供要讀取的檔案的名稱。
  • 路徑:這是一個可選參數,是一個布林值,如果需要在提供的路徑中搜尋文件,則可以將其設為 true 或 false。
  • context: 這也是一個可選字段,用於指定文件句柄的上下文。基本上,上下文是能夠更改該流的行為的物件的集合。如果成功則傳回從檔案讀取的位元組數,如果讀取失敗則傳回 false。

PHP 讀取檔案的方法

除了 readfile() 函數之外,以下是一些可用來對檔案執行各種操作的其他函數。

1.文件()

此函數也用於讀取文件,它將完整的文件讀取到陣列中。

文法:

file ( string $file_name [, int $flag = 0 [, resource $context ]] ) : array

其中file_name是要讀取的檔案的路徑。標誌是可選字段,可以從以下常數中選擇:

  • FILE_USE_INCLUDE_PATH: 在給定路徑中搜尋對應的檔案。
  • FILE_IGNORE_NEW_LINES: 在每個陣列元素的最後省略一新行。
  • FILE_SKIP_EMPTY_LINES: 跳過空白行。

成功時傳回數組中存在的文件,失敗時傳回 false。

2. fopen()

此函數可用於開啟文件和 URL。

fopen ( string $file_name , string $mode [, bool $use_include_path = FALSE [, resource $context ]] ) : resource
  • 如果 file_name 是 URL,則 PHP 會在協定處理程序(也稱為包裝器)中搜尋它。 PHP 將發出通知來幫助追蹤腳本中可能存在的問題,然後繼續將其視為正常的常規 file_name。
  • 假設如果 file_name 是本機文件,則 PHP 嘗試在該文件上開啟相同的流。僅當授予其存取所需的權限時,PHP 才能存取該檔案。
  • 假設給定的 file_name 是一個已註冊的協議,並且如果協議已註冊為網路 URL,則第一個 PHP 確保啟用了allow_url_fopen。如果停用,它將發出警告並失敗。

模式:此參數提到了需要授予流的存取權限類型。以下是一些重要的模式:

  • r – 僅讀取模式
  • r+ – 只能讀寫
  • w – 僅寫入模式

如果成功則傳回檔案指標資源,如果失敗則傳回 false。

3. fread()

此函數用於二進位安全檔案讀取。

文法:

fread ( resource $handle , int $length ) : string

句柄用來引用檔案指標。

檔案將被讀取,直到達到以下條件之一:長度(以位元組為單位)必須已讀取、達到 EOF、發生套接字逾時。 fgets()、fscanf()、ftell()、fwrite()、fopen()、fsockopen() 是 PHP 中用於檔案操作的其他一些函數。

PHP 讀取檔案範例

下面給的是 PHP 讀取檔的範例:

範例#1

代碼:

<?php // it is writing content of file to output
// buffer used in readfile() function
echo readfile("file.txt");
?>

輸出:

PHP讀取文件

這顯示了讀取本機路徑中存在的檔案的基本範例。確保建立了 readfile() 函數參數中指定的檔案名,並且要讀取的內容存在於檔案內。當使用 readfile() 函數時,它會讀取檔案的內容並在輸出中列印。

範例#2

代碼:

<?php / file contents written on output
// buffer by readfile() function
$file = @readfile("file.txt");
if (!$file)
{
print "File could not be opened";
}
?>

輸出:

PHP讀取文件

之前的輸出是一個沒有任何條件的簡單範例。在此範例中,讓我們看看如何使用某些條件讀取並顯示檔案的輸出。我們使用 if 語句來列印假設檔案不存在。

範例 #3

代碼:

<?php $file_name = "file.txt";
$fh = fopen($file_name, 'r');
$data = fread($fh, filesize($file_name));
fclose($fh);
echo $data;
?>

輸出:

PHP讀取文件

In this example, we are combining the use of multiple file read functions. As in all the above examples, first, we are giving the file name which needs to be read from. Then the mode of operation, ‘r’ is given to them indicating it can only be read. filesize() function takes the filename and returns the size of the file along with its data and assigns it to $data variable. By using fclose() function we are closing that file. Finally, the data is printed as output.

Example #4

Code:

<?php $file_name = "file.txt";
$file = fopen( $file_name , "r" );
if( $file == false ) {
echo ( "Error in opening file" );
exit();
}
$size = filesize( $file_name );
$filetext = fread( $file, $size);
fclose( $file );
echo ( "The size of input file in bytes is : $size\n" );
echo ("Printing details of file:\n");
echo ( $filetext );
?>

Output:

PHP讀取文件

Before running the code, make sure that the file to be read file.txt is created in the local file path. In this example first, we are declaring the file name to be read and opening that with the function fopen(). Suppose the file does not exist, using if condition we are throwing an error message. Finally, we are printing the file size and the content present in the input file.

Conclusion

As seen from all the above examples, readfile() is one of the main functions of PHP used for reading the file name specified in this function. Apart from readfile() we have covered a few other file operations which perform similar actions such as fopen, file, fread, fgets, fgetss, ftell, etc. A combination of all of these are basically used in accessing and performing operations on the input file.

以上是PHP讀取文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何檢查PHP會話是否已經開始?如何檢查PHP會話是否已經開始?Apr 30, 2025 am 12:20 AM

在PHP中,可以使用session_status()或session_id()來檢查會話是否已啟動。 1)使用session_status()函數,如果返回PHP_SESSION_ACTIVE,則會話已啟動。 2)使用session_id()函數,如果返回非空字符串,則會話已啟動。這兩種方法都能有效地檢查會話狀態,選擇使用哪種方法取決於PHP版本和個人偏好。

描述一個場景,其中使用會話在Web應用程序中至關重要。描述一個場景,其中使用會話在Web應用程序中至關重要。Apr 30, 2025 am 12:16 AM

sessionsarevitalinwebapplications,尤其是在commercePlatform之前。

如何管理PHP中的並發會話訪問?如何管理PHP中的並發會話訪問?Apr 30, 2025 am 12:11 AM

在PHP中管理並發會話訪問可以通過以下方法:1.使用數據庫存儲會話數據,2.採用Redis或Memcached,3.實施會話鎖定策略。這些方法有助於確保數據一致性和提高並發性能。

使用PHP會話的局限性是什麼?使用PHP會話的局限性是什麼?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

解釋負載平衡如何影響會話管理以及如何解決。解釋負載平衡如何影響會話管理以及如何解決。Apr 29, 2025 am 12:42 AM

負載均衡會影響會話管理,但可以通過會話複製、會話粘性和集中式會話存儲解決。 1.會話複製在服務器間複製會話數據。 2.會話粘性將用戶請求定向到同一服務器。 3.集中式會話存儲使用獨立服務器如Redis存儲會話數據,確保數據共享。

說明會話鎖定的概念。說明會話鎖定的概念。Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

有其他PHP會議的選擇嗎?有其他PHP會議的選擇嗎?Apr 29, 2025 am 12:36 AM

PHP會話的替代方案包括Cookies、Token-basedAuthentication、Database-basedSessions和Redis/Memcached。 1.Cookies通過在客戶端存儲數據來管理會話,簡單但安全性低。 2.Token-basedAuthentication使用令牌驗證用戶,安全性高但需額外邏輯。 3.Database-basedSessions將數據存儲在數據庫中,擴展性好但可能影響性能。 4.Redis/Memcached使用分佈式緩存提高性能和擴展性,但需額外配

在PHP的上下文中定義'會話劫持”一詞。在PHP的上下文中定義'會話劫持”一詞。Apr 29, 2025 am 12:33 AM

Sessionhijacking是指攻擊者通過獲取用戶的sessionID來冒充用戶。防範方法包括:1)使用HTTPS加密通信;2)驗證sessionID的來源;3)使用安全的sessionID生成算法;4)定期更新sessionID。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),