{"id":874,"date":"2012-09-03T13:25:20","date_gmt":"2012-09-03T13:25:20","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=874"},"modified":"2012-09-03T13:27:56","modified_gmt":"2012-09-03T13:27:56","slug":"php-file-handling-tutorial","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/","title":{"rendered":"PHP File Handling Tutorial"},"content":{"rendered":"<p><strong>PHP File handling Tutorial<\/strong><\/p>\n<p>The kinds of things that you will do with files include:<\/p>\n<p>\u2022\u00a0\u00a0 \u00a0Reading and writing text in files.<br \/>\n\u2022\u00a0\u00a0 \u00a0Creating and deleting directories and file manipulation.<\/p>\n<p><strong>Part 1 &#8211; File permissions on the server<\/strong><\/p>\n<p>If you have to write data to a text file on a web server, or you have to copy, rename, delete or do any kind of file manipulation, the file or folder will require correct permissions.<\/p>\n<p>To change file or folder permissions in Dreamweaver, open the site up in the remote view and right click on the file or folder.\u00a0 Select permissions and then choose the kind of permissions that you want.\u00a0 This would be 666 if you want to write and manipulate files.<\/p>\n<p>This may not be necessary, as it may already be set on your web server.<\/p>\n<p><strong>Part 2 &#8211; Working with files<\/strong><\/p>\n<p>The typical way in which you will work with a text file is something like:<\/p>\n<p>\u2022\u00a0\u00a0 \u00a0Open the file that you are going to work with by associating a file handle<br \/>\n\u2022\u00a0\u00a0 \u00a0Read or write to the file using the file handle<br \/>\n\u2022\u00a0\u00a0 \u00a0Close the file using the file handle.<\/p>\n<p>Task 1 &#8211; Opening a file<\/p>\n<p>(1)\u00a0\u00a0 In your text editor create a new PHP web page called file.php.\u00a0 If you are using Dreamweaver, remove all the html code from the page.<\/p>\n<p>(2)\u00a0 Enter the following:<\/p>\n<blockquote><p>&lt;?php<\/p>\n<p>\/\/file.php<br \/>\n$filename = &#8220;data.txt&#8221;;<\/p>\n<p>if\u00a0 (\u00a0 !$fp = fopen($filename, &#8220;r&#8221;) )<br \/>\n{<br \/>\ndie (&#8220;cannot open file $filename&#8221;);<br \/>\n}<\/p>\n<p>fclose($fp);<\/p>\n<p>?&gt;<\/p><\/blockquote>\n<p>The $fp is the file pointer which is used for reading and writing operations.<\/p>\n<p>(3)\u00a0 Upload the file to your web space and run it in your web browser and see what happens.<\/p>\n<p>(4)\u00a0\u00a0 Create a text file called data.txt and upload it to the web space and run file.php in your browser again.<\/p>\n<p>The fopen() function<\/p>\n<p>fopen() takes two arguments.\u00a0 The first argument is the file name.\u00a0 The file name can be relative to the location of this PHP file, or absolute with the full path name of the file in the form \u201c\/myfiles\/www\/data.txt\u201d or it can be a url or an ftp path name to a different server.<\/p>\n<p>The second argument is the mode which can be:<\/p>\n<p>r\u00a0\u00a0 Read only access<br \/>\nr+ Read and write access<br \/>\nw Write access only<br \/>\nw+ Read and write access, any existing data will be lost if the file does not exit and PHP will attempt to create the file<br \/>\na Appending only.\u00a0 Write to the end of the file, if the file does not exist then PHP will attempt to create the file<br \/>\na+ Open for reading and appending. Data is written to the end of the file, if the file does not exist, then PHP will attempt to create it.<\/p>\n<p>Task 2 &#8211; Writing to the file<\/p>\n<p>(1)\u00a0 In file.php, change the mode from r to w+<\/p>\n<p>(2)\u00a0 Before the fclose() line enter the following:<\/p>\n<p>(3)\u00a0 fwrite($fp, \u201cthis is some text);<\/p>\n<p>(4)\u00a0 Your file will now look like:<\/p>\n<blockquote><p>&lt;?php<\/p>\n<p>\/\/file.php<\/p>\n<p>$filename = &#8220;data.txt&#8221;;<\/p>\n<p>if\u00a0 (\u00a0 !$fp = fopen($filename, &#8220;w+&#8221;) )<br \/>\n{<br \/>\ndie (&#8220;cannot open file $data.txt&#8221;);<br \/>\n}<\/p>\n<p>fwrite($fp, &#8220;this is some text&#8221;);<\/p>\n<p>fclose($fp);<\/p>\n<p>?&gt;<\/p><\/blockquote>\n<p>(5)\u00a0 Upload the file to your web server, and run file.php in your web browser.\u00a0 When you examine the data.txt file you should see the extra text.<\/p>\n<p>Task 3 &#8211; Reading from the file<\/p>\n<p>There are a number of ways of reading the contents of file.\u00a0 One method is to read a block of characters into an array until the end of the file is reached<\/p>\n<blockquote><p>while ( !feof($fp) ) {<br \/>\n$lines[] = fgets($fp, 1024);<br \/>\n}<\/p><\/blockquote>\n<p>fgets($fp, 1024) means to read a line with up to 1024 bytes or until an end of line is found.<\/p>\n<p>(1) Modify file.php with the above code added and an extra bit of code to display the text in the array.<\/p>\n<p>The file.php code will now look like:<\/p>\n<blockquote><p>&lt;?php<\/p>\n<p>\/\/file.php<\/p>\n<p>$filename = &#8220;data.txt&#8221;;<\/p>\n<p>if\u00a0 (\u00a0 !$fp = fopen(&#8220;data.txt&#8221;, &#8220;w+&#8221;) )<br \/>\n{<br \/>\ndie (&#8220;cannot open file $data.txt&#8221;);<br \/>\n}<\/p>\n<p>fwrite($fp, &#8220;this is some text\\r\\n&#8221;);<br \/>\nfwrite($fp, &#8220;this is some more text\\r\\n&#8221;);<br \/>\nfwrite($fp, &#8220;this is even more text\\r\\n&#8221;);<\/p>\n<p>while ( !feof($fp) ) {<br \/>\n$lines[] = fgets($fp, 1024);<br \/>\n}<\/p>\n<p>fclose($fp);<\/p>\n<p>foreach ($lines as $key =&gt; $value) {<br \/>\necho($value . &#8220;&lt;br\/&gt;&#8221;);<br \/>\n}<br \/>\n?&gt;<\/p><\/blockquote>\n<p>(2) Upload the file to the web server and run it in your web browser.\u00a0 It should display the text that has been entered into the file.<br \/>\nPart 3 &#8211; File and folder manipulation<\/p>\n<p>There are a number of PHP functions that can be used to work with files and folders.<\/p>\n<p>Some of these functions may need permissions to be modified on the server to allow.<\/p>\n<p><strong>copy() <\/strong> copy a file<br \/>\n<strong>rename()<\/strong> rename a file<br \/>\n<strong>file_exists()<\/strong> shows if the file exists<br \/>\n<strong>basename() <\/strong> returns the base part of a file name<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP File handling Tutorial The kinds of things that you will do with files include: \u2022\u00a0\u00a0 \u00a0Reading and writing text in files. \u2022\u00a0\u00a0 \u00a0Creating and deleting directories and file manipulation. Part 1 &#8211; File permissions on the server If you<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/\">Read more <span class=\"screen-reader-text\">PHP File Handling Tutorial<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-874","post","type-post","status-publish","format-standard","hentry","category-general-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP File Handling Tutorial - PHP Web Applications<\/title>\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.withinweb.com\/info\/php-file-handling-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP File Handling Tutorial - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"PHP File handling Tutorial The kinds of things that you will do with files include: \u2022\u00a0\u00a0 \u00a0Reading and writing text in files. \u2022\u00a0\u00a0 \u00a0Creating and deleting directories and file manipulation. Part 1 &#8211; File permissions on the server If you&hellip;Read more PHP File Handling Tutorial &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-03T13:25:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-09-03T13:27:56+00:00\" \/>\n<meta name=\"author\" content=\"paulv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"paulv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"PHP File Handling Tutorial\",\"datePublished\":\"2012-09-03T13:25:20+00:00\",\"dateModified\":\"2012-09-03T13:27:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/\"},\"wordCount\":790,\"commentCount\":0,\"articleSection\":[\"General PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/\",\"name\":\"PHP File Handling Tutorial - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-09-03T13:25:20+00:00\",\"dateModified\":\"2012-09-03T13:27:56+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/php-file-handling-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP File Handling Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\",\"name\":\"PHP Web Applications\",\"description\":\"Information and support for products of WithinWeb.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\",\"name\":\"paulv\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/author\\\/paulv\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP File Handling Tutorial - PHP Web Applications","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.withinweb.com\/info\/php-file-handling-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"PHP File Handling Tutorial - PHP Web Applications","og_description":"PHP File handling Tutorial The kinds of things that you will do with files include: \u2022\u00a0\u00a0 \u00a0Reading and writing text in files. \u2022\u00a0\u00a0 \u00a0Creating and deleting directories and file manipulation. Part 1 &#8211; File permissions on the server If you&hellip;Read more PHP File Handling Tutorial &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/","og_site_name":"PHP Web Applications","article_published_time":"2012-09-03T13:25:20+00:00","article_modified_time":"2012-09-03T13:27:56+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"PHP File Handling Tutorial","datePublished":"2012-09-03T13:25:20+00:00","dateModified":"2012-09-03T13:27:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/"},"wordCount":790,"commentCount":0,"articleSection":["General PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/","url":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/","name":"PHP File Handling Tutorial - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-09-03T13:25:20+00:00","dateModified":"2012-09-03T13:27:56+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/php-file-handling-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"PHP File Handling Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.withinweb.com\/info\/#website","url":"https:\/\/www.withinweb.com\/info\/","name":"PHP Web Applications","description":"Information and support for products of WithinWeb.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.withinweb.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c","name":"paulv","url":"https:\/\/www.withinweb.com\/info\/author\/paulv\/"}]}},"_links":{"self":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/comments?post=874"}],"version-history":[{"count":6,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/874\/revisions"}],"predecessor-version":[{"id":880,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/874\/revisions\/880"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}