{"id":8816,"date":"2020-06-01T11:00:37","date_gmt":"2020-06-01T02:00:37","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=8816"},"modified":"2020-05-29T19:34:47","modified_gmt":"2020-05-29T10:34:47","slug":"file-operations-in-php","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/8816","title":{"rendered":"File Operations in PHP"},"content":{"rendered":"\n<p>I will talk about file operations in PHP( handling , read, write etc.). Lets take a look at reading file first.<\/p>\n\n\n\n<p>As an example we have a file called <strong>test.txt<\/strong> and if we want to read the contents within the file, can simply do like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\necho readfile(\"test.txt\");\n\/\/this will out put the contents within the file\n?><\/code><\/pre>\n\n\n\n<p>But what if we want to handle more than reading a file, we can use <code><strong>fopen<\/strong><\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/we have to open the file first along with the operation mode.\n\/\/(r,w,a,x) etc. you can see more detail as below. \n\/\/ https:\/\/www.php.net\/manual\/en\/function.fopen.php\n$file = fopen(\"test.txt\", \"r\") or die(\"Unable to open file!\");\n\/\/then we read the file by specifying maximum number of bytes to read\necho fread($file,filesize(\"test.txt\"));\n\/\/ then simply close the file.\nfclose($file);\n?><\/code><\/pre>\n\n\n\n<p>We can also get the first line in file using <code><strong>fgets<\/strong><\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$file = fopen(\"test.txt\", \"r\") or die(\"Unable to open file!\");\necho fgets($file);\nfclose($file);\n?><\/code><\/pre>\n\n\n\n<p>We can also use <code><strong>feof<\/strong><\/code> to check whether the file reaches to the end.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$file = fopen(\"test.txt\", \"r\") or die(\"Unable to open file!\");\n\/\/ Output until end-of-file\nwhile(!feof($file)) {\n  echo fgets($file) . \"&lt;br>\";\n}\nfclose($file);\n?><\/code><\/pre>\n\n\n\n<p>If we want to check whether file exists or not , we can do like this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\necho file_exists(\"test.txt\");\n\/\/if exists, it will return 1\n?><\/code><\/pre>\n\n\n\n<p>By Yuuma.<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8816\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/8816\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"File Operations in PHP\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8816\" data-text=\"File Operations in PHP\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8816\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8816\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>I will talk about file operations in PHP( handling , read, write etc.). Lets take a look at reading file first [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8816"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=8816"}],"version-history":[{"count":5,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8816\/revisions"}],"predecessor-version":[{"id":8825,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8816\/revisions\/8825"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=8816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=8816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=8816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}