<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":4515,"date":"2022-08-01T05:45:29","date_gmt":"2022-08-01T05:45:29","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=4515"},"modified":"2022-08-01T05:45:29","modified_gmt":"2022-08-01T05:45:29","slug":"how-to-read-a-file-in-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/","title":{"rendered":"How to read a file in PHP ?"},"content":{"rendered":"<ul>\n<li style=\"text-align: justify;\"><a href=\"https:\/\/www.wikitechy.com\/php\/\">PHP<\/a> File provides various functions to read data.<\/li>\n<li style=\"text-align: justify;\">There are different functions that allow you to read all file data, read data character by character and read data line by line.<\/li>\n<li style=\"text-align: justify;\">The PHP file available read functions are given below, they are:<\/li>\n<\/ul>\n<ol style=\"text-align: justify;\">\n<li style=\"list-style-type: none;\">\n<ol>\n<li>fread ()<\/li>\n<li>fgets ()<\/li>\n<li>fgetc ()<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h2 id=\"php-read-file-fread\" style=\"text-align: justify;\"><strong>PHP Read File- fread ()<\/strong><\/h2>\n<ul style=\"text-align: justify;\">\n<li>PHP fread () function is used to read data of the file and it consists of two arguments, they are file size and file resource.<\/li>\n<\/ul>\n<h3 id=\"sample-code\" style=\"text-align: justify;\"><strong>Sample Code<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php    <br\/>$filename = &quot;c:\\\\file1.txt&quot;;    <br\/>$fp = fopen($filename, &quot;r&quot;);\/\/open file in read mode    <br\/>  <br\/>$contents = fread($fp, filesize($filename));\/\/read file    <br\/>  <br\/>echo &quot;&lt;pre&gt;$contents&lt;\/pre&gt;&quot;;\/\/printing data of file  <br\/>fclose($fp);\/\/close file    <br\/>?&gt;   <\/code><\/pre> <\/div>\n<h3 id=\"output\" style=\"text-align: justify;\"><strong>Output<\/strong><\/h3>\n<p style=\"text-align: justify;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-4516\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png\" alt=\"\" width=\"1425\" height=\"261\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png 1425w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function-300x55.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function-1024x188.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function-768x141.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function-390x71.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function-820x150.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function-1180x216.png 1180w\" sizes=\"(max-width: 1425px) 100vw, 1425px\" \/><\/p>\n<h2 id=\"php-read-file-fgets\" style=\"text-align: justify;\">PHP Read file- fgets ()<\/h2>\n<ul style=\"text-align: justify;\">\n<li>To read single line from the file PHP fgets () function is used.<\/li>\n<\/ul>\n<h3 id=\"sample-code-2\" style=\"text-align: justify;\"><strong>Sample Code<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php    <br\/>$fp = fopen(&quot;c:\\\\file1.txt&quot;, &quot;r&quot;);\/\/open file in read mode    <br\/>echo fgets($fp);  <br\/>fclose($fp);  <br\/>?&gt;   <\/code><\/pre> <\/div>\n<h3 id=\"output-2\" style=\"text-align: justify;\"><strong>Output<\/strong><\/h3>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4517\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgets-function.png\" alt=\"\" width=\"1430\" height=\"151\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgets-function.png 1430w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgets-function-300x32.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgets-function-1024x108.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgets-function-768x81.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgets-function-390x41.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgets-function-820x87.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgets-function-1180x125.png 1180w\" sizes=\"(max-width: 1430px) 100vw, 1430px\" \/><\/p>\n<h2 id=\"php-read-file-fgetc\" style=\"text-align: justify;\">PHP Read file- fgetc ()<\/h2>\n<ul style=\"text-align: justify;\">\n<li>To read single character from the file PHP fgetc () function is used and use! feof () function inside the while loop to get all data using fgetc () function.<\/li>\n<\/ul>\n<h3 id=\"sample-code-3\" style=\"text-align: justify;\"><strong>Sample Code<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php    <br\/>$fp = fopen(&quot;c:\\\\file1.txt&quot;, &quot;r&quot;);\/\/open file in read mode    <br\/>while(!feof($fp)) {  <br\/>  echo fgetc($fp);  <br\/>}  <br\/>fclose($fp);  <br\/>?&gt;    <\/code><\/pre> <\/div>\n<h3 id=\"output-3\" style=\"text-align: justify;\"><strong>Output<\/strong><\/h3>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4518\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgetc-function.png\" alt=\"\" width=\"1424\" height=\"150\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgetc-function.png 1424w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgetc-function-300x32.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgetc-function-1024x108.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgetc-function-768x81.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgetc-function-390x41.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgetc-function-820x86.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fgetc-function-1180x124.png 1180w\" sizes=\"(max-width: 1424px) 100vw, 1424px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP File provides various functions to read data. There are different functions that allow you to read all file data, read data character by character and read data line by line. The PHP file available read functions are given below, they are: fread () fgets () fgetc () PHP Read File- fread () PHP fread [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[8182],"tags":[17611,17614,17606,17612,17613,17602,17616,17605,17604,17610,17603,17608,17607,17609,17615],"class_list":["post-4515","post","type-post","status-publish","format-standard","hentry","category-php","tag-file-reading-and-writing-in-php","tag-fopen-php","tag-how-to-read-a-file-in-php","tag-how-to-read-a-file-in-php-file","tag-php-file-example","tag-php-file-open-read-close","tag-php-file-reader","tag-php-files-i-o","tag-php-read-file","tag-php-read-text-file-line-by-line","tag-php-readfile-function","tag-read-a-file-in-php","tag-read-a-plain-text-file-with-php","tag-read-text-files-in-php","tag-writing-files-in-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to read a file in PHP ? - PHP readfile() Function - PHP Read File<\/title>\n<meta name=\"description\" content=\"How to read a file in PHP ? - From file PHP provides various functions to read data.There are different functions that allow you to read all file data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to read a file in PHP ? - PHP readfile() Function - PHP Read File\" \/>\n<meta property=\"og:description\" content=\"How to read a file in PHP ? - From file PHP provides various functions to read data.There are different functions that allow you to read all file data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-01T05:45:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png\" \/>\n<meta name=\"author\" content=\"webmaster\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webmaster\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/\",\"name\":\"How to read a file in PHP ? - PHP readfile() Function - PHP Read File\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png\",\"datePublished\":\"2022-08-01T05:45:29+00:00\",\"dateModified\":\"2022-08-01T05:45:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\"},\"description\":\"How to read a file in PHP ? - From file PHP provides various functions to read data.There are different functions that allow you to read all file data.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/#primaryimage\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png\",\"contentUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png\",\"width\":1425,\"height\":261},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\",\"name\":\"webmaster\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"caption\":\"webmaster\"},\"sameAs\":[\"https:\/\/www.wikitechy.com\/interview-questions\"],\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to read a file in PHP ? - PHP readfile() Function - PHP Read File","description":"How to read a file in PHP ? - From file PHP provides various functions to read data.There are different functions that allow you to read all file data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/","og_locale":"en_US","og_type":"article","og_title":"How to read a file in PHP ? - PHP readfile() Function - PHP Read File","og_description":"How to read a file in PHP ? - From file PHP provides various functions to read data.There are different functions that allow you to read all file data.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/","og_site_name":"Wikitechy","article_published_time":"2022-08-01T05:45:29+00:00","og_image":[{"url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png"}],"author":"webmaster","twitter_card":"summary_large_image","twitter_misc":{"Written by":"webmaster","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/","name":"How to read a file in PHP ? - PHP readfile() Function - PHP Read File","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png","datePublished":"2022-08-01T05:45:29+00:00","dateModified":"2022-08-01T05:45:29+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"description":"How to read a file in PHP ? - From file PHP provides various functions to read data.There are different functions that allow you to read all file data.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-read-a-file-in-php\/#primaryimage","url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png","contentUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/fread-function.png","width":1425,"height":261},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4","name":"webmaster","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","caption":"webmaster"},"sameAs":["https:\/\/www.wikitechy.com\/interview-questions"],"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=4515"}],"version-history":[{"count":1,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4515\/revisions"}],"predecessor-version":[{"id":4519,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4515\/revisions\/4519"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=4515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=4515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=4515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}