{"id":1665,"date":"2009-08-10T22:55:18","date_gmt":"2009-08-10T22:55:18","guid":{"rendered":"http:\/\/localhost\/helpcentre\/?p=1665"},"modified":"2025-12-22T15:03:04","modified_gmt":"2025-12-22T13:03:04","slug":"how-do-i-access-and-modify-information-in-a-file-using-php","status":"publish","type":"post","link":"https:\/\/xneelo.co.za\/help-centre\/website\/how-do-i-access-and-modify-information-in-a-file-using-php\/","title":{"rendered":"How do I access and modify information in a file using PHP?"},"content":{"rendered":"<p><b>PHP <\/b>also allows you full ability to read and write to files in your account. Files are opened via the fopen command, which is given in the form:<\/p>\n<div class=\"codeblock\"><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">$FILE <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">fopen<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">\"filename\"<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">\"mode\"<\/span><span style=\"color: #007700;\">); <\/span><br \/>\n<\/span><br \/>\n<\/code><\/div>\n<p>where $FILE is the variable you\u2019ll use to refer to the open file, filename is the name of the file to be opened, and, mode determines level of access to the file using one of the following values:<\/p>\n<ul>\n<li>r (read only)<\/li>\n<li>r+ (read and write)<\/li>\n<li>w (write only)<\/li>\n<li>w+ (read and write, truncate file to 0 bytes)<\/li>\n<li>a (write only, start at end of file \u2013 append)<\/li>\n<li>a+ (read and write, starting at file end)<\/li>\n<\/ul>\n<p>Once the file is opened, there are two commands to read in data. fgetc retrieves a single character, while fgets retrieves a number of bytes you specify. They are used in the following manner:<\/p>\n<div class=\"codeblock\"><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">$ONECHAR <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">fgetc<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$FILE<\/span><span style=\"color: #007700;\">);<br \/>\n<\/span><span style=\"color: #0000bb;\">$TENBYTES <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">fgets<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$FILE<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #0000bb;\">10<\/span><span style=\"color: #007700;\">); <\/span><br \/>\n<\/span><br \/>\n<\/code><\/div>\n<p>If you need to write to the file, the function used is fputs:<\/p>\n<div class=\"codeblock\"><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">fputs<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$FILE<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">\"This text is written to the file\"<\/span><span style=\"color: #007700;\">); <\/span><br \/>\n<\/span><br \/>\n<\/code><\/div>\n<p>After you are done interfacing with the file, you must close your file descriptor with the fclose command:<\/p>\n<div class=\"codeblock\"><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">Fclose <\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$FILE<\/span><span style=\"color: #007700;\">); <\/span><br \/>\n<\/span><br \/>\n<\/code><\/div>\n<p>The sample code below uses the file functions and a while loop to copy the contents of data.txt to newdata.txt (not particularly useful in \u201creal life\u201d, but good for demonstrative purposes).<\/p>\n<div class=\"codeblock\"><code><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #007700;\">\/ <\/span><span style=\"color: #0000bb;\">open data<\/span><span style=\"color: #007700;\">.<\/span><span style=\"color: #0000bb;\">txt <\/span><span style=\"color: #007700;\">for <\/span><span style=\"color: #0000bb;\">reading<br \/>\n$FILE <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">fopen<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">\"\/usr\/home\/username\/data.txt\"<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">\"r\"<\/span><span style=\"color: #007700;\">);<\/span><\/span><\/code><\/code><code><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ open newdata.txt for writing<br \/>\n<\/span><span style=\"color: #0000bb;\">$NEWFILE <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">fopen<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">\"\/usr\/home\/username\/newdata.txt\"<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">\"w\"<\/span><span style=\"color: #007700;\">);<\/span><\/span><\/code><\/code><\/p>\n<p><code><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ continously read in from data.txt<br \/>\n<\/span><span style=\"color: #007700;\">while (<\/span><span style=\"color: #0000bb;\">$BUFFER <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">fgets<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$FILE<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #0000bb;\">4096<\/span><span style=\"color: #007700;\">)) <\/span><span style=\"color: #0000bb;\">{<br \/>\n<\/span><span style=\"color: #ff8000;\">\/\/ write line to newdata.txt<br \/>\n<\/span><span style=\"color: #0000bb;\">fputs<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$NEWFILE<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #0000bb;\">$BUFFER<\/span><span style=\"color: #007700;\">);<br \/>\n<\/span><span style=\"color: #0000bb;\">}<\/span><\/span><\/code><\/code><\/p>\n<p><code><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ close data.txt<br \/>\n<\/span><span style=\"color: #0000bb;\">fclose<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$FILE<\/span><span style=\"color: #007700;\">);<\/span><\/span><\/code><\/code><\/p>\n<p><code><span style=\"color: #000000;\"><span style=\"color: #ff8000;\">\/\/ close newdata.txt<br \/>\n<\/span><span style=\"color: #0000bb;\">fclose<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$NEWFILE<\/span><span style=\"color: #007700;\">);<br \/>\n<\/span><span style=\"color: #0000bb;\">?&gt; <\/span><br \/>\n<\/span><br \/>\n<\/code><\/p>\n<\/div>\n<p>Please note that fopen is restricted to local files only.<\/p>\n","protected":false,"plain":"<b>PHP <\/b>also allows you full ability to read and write to files in your account. Files are opened via the fopen command, which is given in the form:\r\n<div class=\"codeblock\"><code><span >\r\n<span >$FILE <\/span><span >= <\/span><span >fopen<\/span><span >(<\/span><span >\"filename\"<\/span><span >,<\/span><span >\"mode\"<\/span><span >); <\/span>\r\n<\/span>\r\n<\/code><\/div>\r\nwhere $FILE is the variable you\u2019ll use to refer to the open file, filename is the name of the file to be opened, and, mode determines level of access to the file using one of the following values:\r\n<ul>\r\n \t<li>r (read only)<\/li>\r\n \t<li>r+ (read and write)<\/li>\r\n \t<li>w (write only)<\/li>\r\n \t<li>w+ (read and write, truncate file to 0 bytes)<\/li>\r\n \t<li>a (write only, start at end of file \u2013 append)<\/li>\r\n \t<li>a+ (read and write, starting at file end)<\/li>\r\n<\/ul>\r\nOnce the file is opened, there are two commands to read in data. fgetc retrieves a single character, while fgets retrieves a number of bytes you specify. They are used in the following manner:\r\n<div class=\"codeblock\"><code><span >\r\n<span >$ONECHAR <\/span><span >= <\/span><span >fgetc<\/span><span >(<\/span><span >$FILE<\/span><span >);\r\n<\/span><span >$TENBYTES <\/span><span >= <\/span><span >fgets<\/span><span >(<\/span><span >$FILE<\/span><span >,<\/span><span >10<\/span><span >); <\/span>\r\n<\/span>\r\n<\/code><\/div>\r\nIf you need to write to the file, the function used is fputs:\r\n<div class=\"codeblock\"><code><span >\r\n<span >fputs<\/span><span >(<\/span><span >$FILE<\/span><span >,<\/span><span >\"This text is written to the file\"<\/span><span >); <\/span>\r\n<\/span>\r\n<\/code><\/div>\r\nAfter you are done interfacing with the file, you must close your file descriptor with the fclose command:\r\n<div class=\"codeblock\"><code><span >\r\n<span >Fclose <\/span><span >(<\/span><span >$FILE<\/span><span >); <\/span>\r\n<\/span>\r\n<\/code><\/div>\r\nThe sample code below uses the file functions and a while loop to copy the contents of data.txt to newdata.txt (not particularly useful in \u201creal life\u201d, but good for demonstrative purposes).\r\n<div class=\"codeblock\"><code><code><span >\r\n<span >\/ <\/span><span >open data<\/span><span >.<\/span><span >txt <\/span><span >for <\/span><span >reading\r\n$FILE <\/span><span >= <\/span><span >fopen<\/span><span >(<\/span><span >\"\/usr\/home\/username\/data.txt\"<\/span><span >,<\/span><span >\"r\"<\/span><span >);<\/span><\/span><\/code><\/code><code><code><span ><span >\/\/ open newdata.txt for writing\r\n<\/span><span >$NEWFILE <\/span><span >= <\/span><span >fopen<\/span><span >(<\/span><span >\"\/usr\/home\/username\/newdata.txt\"<\/span><span >,<\/span><span >\"w\"<\/span><span >);<\/span><\/span><\/code><\/code>\r\n\r\n<code><code><span ><span >\/\/ continously read in from data.txt\r\n<\/span><span >while (<\/span><span >$BUFFER <\/span><span >= <\/span><span >fgets<\/span><span >(<\/span><span >$FILE<\/span><span >,<\/span><span >4096<\/span><span >)) <\/span><span >{\r\n<\/span><span >\/\/ write line to newdata.txt\r\n<\/span><span >fputs<\/span><span >(<\/span><span >$NEWFILE<\/span><span >,<\/span><span >$BUFFER<\/span><span >);\r\n<\/span><span >}<\/span><\/span><\/code><\/code>\r\n\r\n<code><code><span ><span >\/\/ close data.txt\r\n<\/span><span >fclose<\/span><span >(<\/span><span >$FILE<\/span><span >);<\/span><\/span><\/code><\/code>\r\n\r\n<code><span ><span >\/\/ close newdata.txt\r\n<\/span><span >fclose<\/span><span >(<\/span><span >$NEWFILE<\/span><span >);\r\n<\/span><span >?&gt; <\/span>\r\n<\/span>\r\n<\/code>\r\n\r\n<\/div>\r\nPlease note that fopen is restricted to local files only."},"excerpt":{"rendered":"<p>PHP also allows you full ability to read and write to files in your account. Files are opened via the fopen command, an example is provided in this article.<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"lsx_disable_title":"0","_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","footnotes":""},"categories":[168,179,166],"tags":[24111,24108,17168],"topics":[],"class_list":["post-1665","post","type-post","status-publish","format-standard","hentry","category-managing-website","category-php","category-website","tag-fopen","tag-modify-information","tag-php"],"acf":[],"additional_meta":{"category_title":[{"term_id":168,"name":"Managing your Website","slug":"managing-website","term_group":0,"term_taxonomy_id":168,"taxonomy":"category","description":"Managing your Website","parent":166,"count":52,"filter":"raw","term_order":"83","cat_ID":168,"category_count":52,"category_description":"Managing your Website","cat_name":"Managing your Website","category_nicename":"managing-website","category_parent":166},{"term_id":179,"name":"PHP","slug":"php","term_group":0,"term_taxonomy_id":179,"taxonomy":"category","description":"Allowing you to create dynamic content that interacts with databases","parent":168,"count":20,"filter":"raw","term_order":"97","cat_ID":179,"category_count":20,"category_description":"Allowing you to create dynamic content that interacts with databases","cat_name":"PHP","category_nicename":"php","category_parent":168},{"term_id":166,"name":"Website","slug":"website","term_group":0,"term_taxonomy_id":166,"taxonomy":"category","description":"About your Website(s)","parent":0,"count":169,"filter":"raw","term_order":"120","cat_ID":166,"category_count":169,"category_description":"About your Website(s)","cat_name":"Website","category_nicename":"website","category_parent":0}],"tag_title":[{"term_id":24111,"name":"fopen","slug":"fopen","term_group":0,"term_taxonomy_id":24111,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw","term_order":"112"},{"term_id":24108,"name":"modify information","slug":"modify-information","term_group":0,"term_taxonomy_id":24108,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw","term_order":"113"},{"term_id":17168,"name":"php","slug":"php","term_group":0,"term_taxonomy_id":17168,"taxonomy":"post_tag","description":"","parent":0,"count":16,"filter":"raw","term_order":"2390"}]},"featured_image_src":null,"author_info":{"display_name":"marketing","author_link":"https:\/\/xneelo.co.za\/help-centre\/author\/marketing\/","author_avatar":"https:\/\/secure.gravatar.com\/avatar\/a6ea315e112423b2b955cb020fbce2b0835956c6ad85ff0f13f1db298977eaaa?s=96&d=mm&r=g"},"_links":{"self":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts\/1665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/comments?post=1665"}],"version-history":[{"count":0,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/posts\/1665\/revisions"}],"wp:attachment":[{"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/media?parent=1665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/categories?post=1665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/tags?post=1665"},{"taxonomy":"topics","embeddable":true,"href":"https:\/\/xneelo.co.za\/help-centre\/wp-json\/wp\/v2\/topics?post=1665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}