{"id":116,"date":"2026-05-20T10:56:06","date_gmt":"2026-05-20T10:56:06","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=116"},"modified":"2026-05-21T14:40:05","modified_gmt":"2026-05-21T14:40:05","slug":"file-read-write","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=file-read-write","title":{"rendered":"File Read\/Write"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File handling is an essential part of programming that allows applications to create, read, update, and delete files. File Read\/Write operations help developers store data permanently and manage information efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In PHP, file handling functions make it easy to work with text files, logs, reports, configuration files, and user-generated content.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand file handling concepts<\/li>\n\n\n\n<li>Create and open files in PHP<\/li>\n\n\n\n<li>Read data from files<\/li>\n\n\n\n<li>Write and append data to files<\/li>\n\n\n\n<li>Close files properly<\/li>\n\n\n\n<li>Check file existence and permissions<\/li>\n\n\n\n<li>Handle file operations safely<\/li>\n\n\n\n<li>Build simple file management applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is File Read\/Write<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File Read\/Write refers to the process of accessing file content for reading information or storing data into files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">File Reading<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to retrieve data stored inside a file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">File Writing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to create new content or update existing file content.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Importance of File Handling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File handling is important because it helps developers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Store data permanently<\/li>\n\n\n\n<li>Generate reports and logs<\/li>\n\n\n\n<li>Save user information<\/li>\n\n\n\n<li>Manage application settings<\/li>\n\n\n\n<li>Process uploaded files<\/li>\n\n\n\n<li>Build data-driven applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Common PHP File Functions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">fopen()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to open a file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$file = fopen(\"data.txt\", \"r\");<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">fclose()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to close a file after use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>fclose($file);<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">fread()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Reads data from a file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$file = fopen(\"data.txt\", \"r\");<br>echo fread($file, filesize(\"data.txt\"));<br>fclose($file);<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">fwrite()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Writes data into a file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$file = fopen(\"data.txt\", \"w\");<br>fwrite($file, \"Welcome to PHP File Handling\");<br>fclose($file);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">File Modes in PHP<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Read Mode<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>r<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Opens a file for reading only.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Write Mode<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>w<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Opens a file for writing and clears existing content.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Append Mode<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Adds new content at the end of the file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Read and Write Mode<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>r+<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Allows both reading and writing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reading Files Line by Line<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$file = fopen(\"data.txt\", \"r\");<br><br>while(!feof($file)) {<br>    echo fgets($file);<br>}<br><br>fclose($file);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Appending Data to a File<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$file = fopen(\"data.txt\", \"a\");<br>fwrite($file, \"\\nNew Record Added\");<br>fclose($file);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Checking if a File Exists<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>if(file_exists(\"data.txt\")) {<br>    echo \"File Exists\";<br>} else {<br>    echo \"File Not Found\";<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a New File<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$file = fopen(\"newfile.txt\", \"w\");<br>fclose($file);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting a File<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>unlink(\"oldfile.txt\");<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">File Handling Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always close files after use<\/li>\n\n\n\n<li>Check if files exist before reading<\/li>\n\n\n\n<li>Use proper file permissions<\/li>\n\n\n\n<li>Validate user input for file operations<\/li>\n\n\n\n<li>Avoid overwriting important files<\/li>\n\n\n\n<li>Handle errors properly<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of File Read\/Write<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Permanent data storage<\/li>\n\n\n\n<li>Easy report generation<\/li>\n\n\n\n<li>Improved application management<\/li>\n\n\n\n<li>Efficient log handling<\/li>\n\n\n\n<li>Better data organization<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File handling is used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Student management systems<\/li>\n\n\n\n<li>Attendance systems<\/li>\n\n\n\n<li>Website logs<\/li>\n\n\n\n<li>Report generation tools<\/li>\n\n\n\n<li>Content management systems<\/li>\n\n\n\n<li>Backup systems<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Exercise<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a PHP application that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Creates a text file<\/li>\n\n\n\n<li>Stores student records<\/li>\n\n\n\n<li>Reads and displays stored data<\/li>\n\n\n\n<li>Appends new records<\/li>\n\n\n\n<li>Deletes unwanted files<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Final Presentation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In your final presentation, explain:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What File Read\/Write means<\/li>\n\n\n\n<li>Importance of file handling<\/li>\n\n\n\n<li>Common PHP file functions<\/li>\n\n\n\n<li>File modes and their uses<\/li>\n\n\n\n<li>Reading and writing operations<\/li>\n\n\n\n<li>Real-world applications of file handling<\/li>\n<\/ul>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate PHP > Strings and Files > File Read\/Write<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779274563972\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":34,"template":"","class_list":["post-116","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>File Read\/Write - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn PHP File Read\/Write operations with practical examples including fopen, fread, fwrite, append, and file handling.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=file-read-write\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"File Read\/Write - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn PHP File Read\/Write operations with practical examples including fopen, fread, fwrite, append, and file handling.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=file-read-write\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:40:05+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=file-read-write\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=file-read-write\",\"name\":\"File Read\\\/Write - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T10:56:06+00:00\",\"dateModified\":\"2026-05-21T14:40:05+00:00\",\"description\":\"Learn PHP File Read\\\/Write operations with practical examples including fopen, fread, fwrite, append, and file handling.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=file-read-write#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=file-read-write\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=file-read-write#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate PHP > Strings and Files > File Read\\\/Write\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"File Read\/Write - Learn PHP with GiGz.PK","description":"Learn PHP File Read\/Write operations with practical examples including fopen, fread, fwrite, append, and file handling.","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:\/\/gigz.pk\/php\/?lesson=file-read-write","og_locale":"en_US","og_type":"article","og_title":"File Read\/Write - Learn PHP with GiGz.PK","og_description":"Learn PHP File Read\/Write operations with practical examples including fopen, fread, fwrite, append, and file handling.","og_url":"https:\/\/gigz.pk\/php\/?lesson=file-read-write","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:40:05+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=file-read-write","url":"https:\/\/gigz.pk\/php\/?lesson=file-read-write","name":"File Read\/Write - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T10:56:06+00:00","dateModified":"2026-05-21T14:40:05+00:00","description":"Learn PHP File Read\/Write operations with practical examples including fopen, fread, fwrite, append, and file handling.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=file-read-write#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=file-read-write"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=file-read-write#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Intermediate PHP > Strings and Files > File Read\/Write"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}