{"id":1898,"date":"2021-06-02T10:23:35","date_gmt":"2021-06-02T10:23:35","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1898"},"modified":"2021-09-04T10:39:42","modified_gmt":"2021-09-04T10:39:42","slug":"php-read-file","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-read-file\/","title":{"rendered":"PHP Read File"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to read a file using the various built-in PHP functions.<\/p>\n\n\n\n<p>To read the contents from a file, you follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-open-file\/\">Open the file<\/a> for reading using the <code>fopen()<\/code> function.<\/li><li>Read the contents from the file using the <code>fread()<\/code> function.<\/li><li>Close the file using the <code>fclose()<\/code> function.<\/li><\/ul>\n\n\n\n<p>Here&#8217;s the syntax of the <code>fread()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">fread ( resource $stream , int $length ) : string|<span class=\"hljs-keyword\">false<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>fread()<\/code> function has two parameters: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>$stream<\/code> is a file system pointer resource, which is typically the result of the <code>fopen()<\/code> function.<\/li><li>The <code>$length<\/code> specifies the maximum number of bytes to read. If you want to read the entire file, you can pass the file size to the <code>$length<\/code> parameter.<\/li><\/ul>\n\n\n\n<p>The <code>fread()<\/code> function returns the file contents or <code>false<\/code> if it fails to read.<\/p>\n\n\n\n<p>The <code>fread()<\/code> function stops reading the file once the <code>$length<\/code> number of bytes has been read or the end of file (EOF) has been reached.<\/p>\n\n\n\n<p>To check if the file pointer is at end of file, you can pass it to the <code>feof()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">feof ( resource $stream ) : bool\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>feof()<\/code> function returns <code>true<\/code> if the <code>$stream<\/code> is at the EOF or an error occurs. Otherwise, it returns <code>false<\/code>.<\/p>\n\n\n\n<p>To read a file line by line, you use the <code>fgets()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">fgets ( resource $handle , int $length = ? ) : string|<span class=\"hljs-keyword\">false<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Like the <code>fread()<\/code> function, the <code>fgets()<\/code> function accepts a file system pointer resource and up to a number of bytes to read. If you omit the <code>$length<\/code> argument, the <code>fread()<\/code> function will read the entire line.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-read-file-examples'>PHP read file examples <a href=\"#php-read-file-examples\" class=\"anchor\" id=\"php-read-file-examples\" title=\"Anchor for PHP read file examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of how to read a file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-read-the-entire-file-into-a-string'>1) Read the entire file into a string <a href=\"#1-read-the-entire-file-into-a-string\" class=\"anchor\" id=\"1-read-the-entire-file-into-a-string\" title=\"Anchor for 1) Read the entire file into a string\">#<\/a><\/h3>\n\n\n\n<p>Suppose that you have a file named <code>population.txt<\/code> located at <code>public<\/code> directory with the following contents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">1\tNew York\t\u00a0New York\t8,253,213\n2\tLos Angeles\t\u00a0California\t3,970,219\n3\tChicago\t\u00a0Illinois\t2,677,643\n4\tHouston\t\u00a0Texas\t2,316,120\n5\tPhoenix\t\u00a0Arizona\t1,708,127\n6\tPhiladelphia\u00a0Pennsylvania\t1,578,487\n7\tSan Antonio\t\u00a0Texas\t1,567,118\n8\tSan Diego\t\u00a0California\t1,422,420\n9\tDallas\t\u00a0Texas\t1,343,266\n10\tSan Jose\t\u00a0California\t1,013,616\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example uses the <code>fread()<\/code> function to read the contents of the entire <code>population.txt<\/code> file into a string and shows it on the webpage:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\r\n\r\n$filename = <span class=\"hljs-string\">'.\/public\/population.txt'<\/span>;\r\n$f = fopen($filename, <span class=\"hljs-string\">'r'<\/span>);\r\n\r\n<span class=\"hljs-keyword\">if<\/span> ($f) {\r\n    $contents = fread($f, filesize($filename));\r\n    fclose($f);\r\n    <span class=\"hljs-keyword\">echo<\/span> nl2br($contents);\r\n}\r\n\r<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, open the <code>population.txt<\/code> file using the <code>fopen()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$f = fopen($filename, <span class=\"hljs-string\">'r'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, read the contents of the entire file using the <code>fread()<\/code> function; use the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-filesize\/\">filesize()<\/a><\/code> function to get the size of the file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$contents = fread($f, filesize($filename));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, show the contents of the file on a web page; use the <code>nl2br()<\/code> function to convert the newline characters to <code>&lt;br&gt;<\/code> tags.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">echo<\/span> nl2br($contents);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, close the file using the <code>fclose()<\/code> function.<\/p>\n\n\n\n<p class=\"note\">Note that the <code>file_get_contents()<\/code> function is a shortcut for opening a file, reading the whole file&#8217;s contents into a string, and close it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-read-some-characters-from-a-file'>2) Read some characters from a file <a href=\"#2-read-some-characters-from-a-file\" class=\"anchor\" id=\"2-read-some-characters-from-a-file\" title=\"Anchor for 2) Read some characters from a file\">#<\/a><\/h3>\n\n\n\n<p>To read some characters from a file, you specify the number of bytes to read. The following example uses the <code>fread()<\/code> function to read up to 100 bytes from the <code>population.txt<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\r\n\r\n$filename = <span class=\"hljs-string\">'.\/public\/population.txt'<\/span>;\r\n$f = fopen($filename, <span class=\"hljs-string\">'r'<\/span>);\r\n<span class=\"hljs-keyword\">if<\/span> ($f) {\r\n    $contents = fread($f, <span class=\"hljs-number\">100<\/span>);\r\n    fclose($f);\r\n    <span class=\"hljs-keyword\">echo<\/span> nl2br($contents);\r\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">1 New York  New York 8,253,213\n2 Los Angeles  California 3,970,219\n3 Chicago  Illinois 2,677,64<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='3-read-a-file-line-by-line'>3) Read a file line by line <a href=\"#3-read-a-file-line-by-line\" class=\"anchor\" id=\"3-read-a-file-line-by-line\" title=\"Anchor for 3) Read a file line by line\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>fgets()<\/code> funtion to read the <code>population.txt<\/code> file line by line:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\r\n\r\n$filename = <span class=\"hljs-string\">'.\/public\/population.txt'<\/span>;\r\n$lines = &#91;];\r\n\r\n$f = fopen($filename, <span class=\"hljs-string\">'r'<\/span>);\r\n\r\n<span class=\"hljs-keyword\">if<\/span> (!$f) {\r\n    <span class=\"hljs-keyword\">return<\/span>;\r\n}\r\n\r\n<span class=\"hljs-keyword\">while<\/span> (!feof($f)) {\r\n    $lines&#91;] = fgets($f);\r\n}\r\n\r\nprint_r($lines);\r\n\r\nfclose($f);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>fread()<\/code> function to read some or all contents from a file.<\/li><li>Use the <code>fgets()<\/code> function to read a line from a file.<\/li><li>Use the <code>feof()<\/code> function to test the end-of-file has been reached.<\/li><li>Use the <code>filesize()<\/code> function to get the size of the file.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1898\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-read-file\/\"\n\t\t\t\tdata-post-title=\"PHP Read File\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1898\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-read-file\/\"\n\t\t\t\tdata-post-title=\"PHP Read File\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to read a file using the various built-in PHP functions. To read the contents from a file, you follow these steps: Open the file for reading using the fopen() function. Read the contents from the file using the fread() function. Close the file using the fclose() function. Here&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":144,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1898","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1898"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1898\/revisions"}],"predecessor-version":[{"id":2531,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1898\/revisions\/2531"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}