{"id":80,"date":"2020-12-12T06:57:47","date_gmt":"2020-12-12T06:57:47","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/php\/?p=80"},"modified":"2026-10-29T01:29:04","modified_gmt":"2026-10-29T01:29:04","slug":"php-working-with-files","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/","title":{"rendered":"PHP &#8211; Working with Files"},"content":{"rendered":"<p>In this tutorial, we would see how to manipulate files and directories. The following would be covered:<\/p>\n<ol>\n<li><a href=\"#t1\">Opening and Closing Files<\/a><\/li>\n<li><a href=\"#t2\">Reading File Content<\/a><\/li>\n<li><a href=\"#t3\">Writing to a File<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Opening and Closing Files<\/strong><\/h4>\n<p>You can open a file in PHP using the fopen() function. This function takes two arguments: the file path and the file mode in which it would open. There are six different file modes as specified in the table below:<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th class=\"ts\" width=\"10%\">SN<\/th>\n<th class=\"ts\">Mode &amp; Description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>r\u00a0 <\/b>&#8211; Opens the file for read-only and place the file pointer at the beginning of the file.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>r+ <\/b>&#8211; Opens the file for read\/write and places the file pointer at the beginning of the file.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>w <\/b>&#8211; Opens the file for write only and places the file pointer at the beginning of the file.<\/p>\n<p>the file is truncated to zero length.<\/p>\n<p>If files does not exist then a new file is created.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>w+ <\/b>&#8211; Opens the file for reading and write-only and places the file pointer at the beginning of the file.<\/p>\n<p>the file is truncated to zero length.<\/p>\n<p>If files does not exist then a new file is created.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>a\u00a0<\/b><b> <\/b>&#8211; Opens the file for reading and write-only and places the file pointer at the end of the file.<\/p>\n<p>the file is truncated to zero length.<\/p>\n<p>If files does not exist then a new file is created.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>a+<\/b><\/p>\n<p>Opens the file for reading and appending only and the pointer is placed at the end of the file. The file content is preserved<\/p>\n<p>the file is not truncated to zero length.<\/p>\n<p>If files does not exist then a new file is created.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <strong>fopen()<\/strong> function will return a file pointer for reading the file if the operation is successful. However, if the operation fails, the function returns false. After reading or writing a file, you have to close the file using the <strong>fclose()<\/strong> function. This function returns true if the close operation succeeds, otherwise it returns false. We&#8217;ll an example in the next section.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Reading File Content<\/strong><\/h4>\n<p>Once a file is successfully opened, the content can be read using the <strong>fread()<\/strong> function. This function take two arguments: the first argument is the pointer (returned by <strong>fopen<\/strong>) while the second argument is the length of the file in bytes.<\/p>\n<p>The length of the file can be obtained using the<strong> filesize()<\/strong> function. This function takes two arguments as well: the file name and the file size in bytes.<\/p>\n<p>The steps for reading a file can be summarised as follows:<\/p>\n<ul>\n<li>open the file using the<strong> fopen()<\/strong> funtion<\/li>\n<li>get the length of the file using the <strong>filesize()<\/strong> function<\/li>\n<li>read the content of the file using the <strong>fread()<\/strong> function<\/li>\n<li>close the file using <strong>fclose()<\/strong> function<\/li>\n<\/ul>\n<p>Let&#8217;s take an example that reads the content of a file and displays the content to a web page<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span> \r\n    <span style=\"color: #996633;\">$filePointer<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">fopen<\/span>(<span style=\"background-color: #fff0f0;\">\"names.txt\"<\/span>, <span style=\"background-color: #fff0f0;\">\"r+\"<\/span>);\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> (<span style=\"color: #996633;\">$fileSize<\/span> <span style=\"color: #333333;\">==<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span>){\r\n        <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"Could not open file\"<\/span>;\r\n        <span style=\"color: #008800; font-weight: bold;\">exit<\/span>();\r\n    }\r\n    \r\n    <span style=\"color: #996633;\">$fileSize<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">filesize<\/span>(<span style=\"background-color: #fff0f0;\">\"names.txt\"<\/span>);\r\n    <span style=\"color: #996633;\">$fileContent<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">fread<\/span>(<span style=\"color: #996633;\">$filePointer<\/span>, <span style=\"color: #996633;\">$fileSize<\/span>);\r\n    <span style=\"color: #007020;\">fclose<\/span>(<span style=\"color: #996633;\">$filePointer<\/span>);\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"File size is: <\/span><span style=\"background-color: #eeeeee;\">$fileSize<\/span><span style=\"background-color: #fff0f0;\"> bytes\"<\/span>;\r\n    <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;pre&gt; <\/span><span style=\"background-color: #eeeeee;\">$fileContent<\/span><span style=\"background-color: #fff0f0;\"> &lt;\/pre&gt;\"<\/span>;\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Writing to a File<\/strong><\/h4>\n<p>As mentioned before, you can write some text into a new file or an existing file. You can do this using the<strong> fwrite()<\/strong> function. This function takes two arguments: first is the file pointer and the second is the string of data to be written. There is also a third optional argument, that is the size of data to write. If this argument is specified, then the write will stop once the given size of data is written.<\/p>\n<p>The example below creates a new file and writes some text inside. You can use the function file_exists() to check if a file exists. This function takes the name of the file as an argument.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span> \r\n    <span style=\"color: #996633;\">$filename<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"names.txt\"<\/span>;\r\n    <span style=\"color: #996633;\">$filePointer<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">fopen<\/span>(<span style=\"color: #996633;\">$filePointer<\/span>, <span style=\"background-color: #fff0f0;\">\"w\"<\/span>);\r\n    \r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #996633;\">$filePointer<\/span> <span style=\"color: #333333;\">==<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span>){\r\n        <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"Could not open file\"<\/span>;\r\n        <span style=\"color: #008800; font-weight: bold;\">exit<\/span>();\r\n    }\r\n\r\n    <span style=\"color: #996633;\">$textToWrite<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"This is an example of writing to file\"<\/span>;\r\n\r\n    <span style=\"color: #007020;\">fwrite<\/span>(<span style=\"color: #996633;\">$filePointer<\/span>, <span style=\"color: #996633;\">$textToWrite<\/span>);\r\n    <span style=\"color: #007020;\">fclose<\/span>(<span style=\"color: #996633;\">$filePointer<\/span>);\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we would see how to manipulate files and directories. The following would be covered: Opening and Closing Files Reading File Content Writing &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[12],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-reading-and-writing-to-file"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP - Working with Files | Kindson The Genius<\/title>\n<meta name=\"description\" content=\"PHP - Working with Files \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP - Working with Files | Kindson The Genius\" \/>\n<meta property=\"og:description\" content=\"PHP - Working with Files \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-12T06:57:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-29T01:29:04+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-files\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-files\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"headline\":\"PHP &#8211; Working with Files\",\"datePublished\":\"2020-12-12T06:57:47+00:00\",\"dateModified\":\"2026-10-29T01:29:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-files\\\/\"},\"wordCount\":596,\"keywords\":[\"Reading and Writing to File\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-files\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-files\\\/\",\"name\":\"PHP - Working with Files | Kindson The Genius\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\"},\"datePublished\":\"2020-12-12T06:57:47+00:00\",\"dateModified\":\"2026-10-29T01:29:04+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"description\":\"PHP - Working with Files \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-files\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-files\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-files\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP &#8211; Working with Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\",\"name\":\"PHP Tutorial\",\"description\":\"Beginner to Expert With Practicals\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP - Working with Files | Kindson The Genius","description":"PHP - Working with Files \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","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:\/\/kindsonthegenius.com\/php\/php-working-with-files\/","og_locale":"en_US","og_type":"article","og_title":"PHP - Working with Files | Kindson The Genius","og_description":"PHP - Working with Files \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","og_url":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/","og_site_name":"PHP Tutorial","article_published_time":"2020-12-12T06:57:47+00:00","article_modified_time":"2026-10-29T01:29:04+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"headline":"PHP &#8211; Working with Files","datePublished":"2020-12-12T06:57:47+00:00","dateModified":"2026-10-29T01:29:04+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/"},"wordCount":596,"keywords":["Reading and Writing to File"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/","url":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/","name":"PHP - Working with Files | Kindson The Genius","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/#website"},"datePublished":"2020-12-12T06:57:47+00:00","dateModified":"2026-10-29T01:29:04+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"description":"PHP - Working with Files \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/php\/"},{"@type":"ListItem","position":2,"name":"PHP &#8211; Working with Files"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/php\/#website","url":"https:\/\/kindsonthegenius.com\/php\/","name":"PHP Tutorial","description":"Beginner to Expert With Practicals","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/php\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/80\/revisions\/82"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}