搜尋

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 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SecLists

SecLists

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

mPDF

mPDF

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