{"id":359,"date":"2018-01-13T11:29:21","date_gmt":"2018-01-13T11:29:21","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=359"},"modified":"2018-01-13T11:29:21","modified_gmt":"2018-01-13T11:29:21","slug":"php-file-handling","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/php-file-handling\/","title":{"rendered":"PHP File Handling"},"content":{"rendered":"<p>File handling is used often to read and write the particular file. It is an important part of the web application. In php, you can create a new file, read a particular file, use to upload some on the server and edit files.<\/p>\n<p><strong>readfile() Function<\/strong><\/p>\n<p>The php <code>readfile()<\/code> function reads the file and writes the output in the browser.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>[php]&lt;?php<br \/>\necho readfile(&quot;webdictionary.txt&quot;);<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the code, the readfile() function tells php to read the file name webdictionary.txt.<\/p>\n<p>The output of the echo statement will look like this.<\/p>\n<p><code>AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language236<\/code><\/p>\n<p><strong>fopen() Function<\/strong><\/p>\n<p>The <code>fopen()<\/code> function is more flexibility than <code>readfile()<\/code> function.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>webdictionary.txt file content<\/p>\n<p><code>AJAX = Asynchronous JavaScript and XML<br \/>\nCSS = Cascading Style Sheets<br \/>\nHTML = Hyper Text Markup Language<br \/>\nPHP = PHP Hypertext Preprocessor<br \/>\nSQL = Structured Query Language<br \/>\nSVG = Scalable Vector Graphics<br \/>\nXML = EXtensible Markup Language<\/code><\/p>\n<p><strong>Out php code:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$myfile = fopen(&quot;webdictionary.txt&quot;, &quot;r&quot;) or die(&quot;Unable to open file!&quot;);<br \/>\necho fread($myfile,filesize(&quot;webdictionary.txt&quot;));<br \/>\nfclose($myfile);<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the php code first, we have created variable <code>$myfile<\/code> and assigned it to the <code>fopen()<\/code> function. In the fopon function, we are instructing php script to open the file name &#8220;webdictionary.txt&#8221; and the &#8220;r&#8221; text says to open the file to read only. It means the user can&#8217;t make any changes to the file. <\/p>\n<p>After the closing bracket of the function, we have added condition called die() if the file unable to open.<\/p>\n<p>On the next line, we ask php to echo the result by using <code>fread()<\/code> function and specified the above variable and filename to read.<\/p>\n<p>In the end, we have closed the function by using <code>fclose()<\/code> function.<\/p>\n<p>The above code will generate the following result.<\/p>\n<p><code>AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language<\/code><\/p>\n<p><strong>PHP Modes<\/strong><\/p>\n<p>1) <code>r<\/code> &#8211; The r mode only reads the file. Pointer start at the beginning of the file.<\/p>\n<p>2) <code>w<\/code> &#8211; It opens the file to write only. It is used to erase the content of the file or create a new file if the file doesn&#8217;t already exist in the folder. The pointer of the file starts at the beginning of the file.<\/p>\n<p>3) <code>a<\/code> &#8211; The mode is used to open the file to write only. It preserves the existing data in the file. The file pointer, in this case, starts at the end of the file. It creates a new file automatically if it doesn&#8217;t exist in the folder.<\/p>\n<p>4) <code>x <\/code>&#8211; It creates a new file to write only. It will return the false value with an error if the file already exists in the folder.<\/p>\n<p>5) <code>r+<\/code> &#8211; It opens the file to read or write. The file pointer starts at the beginning of the file.<\/p>\n<p>6) <code>w+<\/code> &#8211; It opens a file to read and write. It erases the content of the file or creates a completely new file if it doesn&#8217;t exist. The file pointer starts at the beginning of the file.<\/p>\n<p>7) <code>a+<\/code> &#8211; The mode used to open a file for reading or writing. It preserves the data of the file. The pointer starts at the end of the file and creates a new file if it doesn&#8217;t exist. <\/p>\n<p>8) <code>x+<\/code> &#8211; It creates a new file for reading or writing. It shows false value with an error if the file doesn&#8217;t exist.<\/p>\n<p><strong>fread() function:<\/strong><\/p>\n<p>The <code>fread()<\/code> function is used to read the open file. It will not open the file. For that, you need to use the <code>fopen()<\/code> function.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p><code>fread($myfile,filesize(\"webdictionary.txt\"));<\/code><\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the above code <code>fread()<\/code> function is asked to read the file name <code>$myfile<\/code>. In the next parameter, it is asked to read a specific number of bytes which in this case is the file name &#8220;webdictionary.txt&#8221;.<\/p>\n<p><strong>fclose() function<\/strong><\/p>\n<p>The <code>fclose()<\/code> function is used to close the open file.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$myfile = fopen(&quot;webdictionary.txt&quot;, &quot;r&quot;);<br \/>\n\/\/ some code to be executed&#8230;.<br \/>\nfclose($myfile);<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code explanation:<\/strong><\/p>\n<p>In the above code we have used <code>fclose()<\/code> function to close the file name <code>$myfile<\/code><\/p>\n<p><strong>fgets() function<\/strong><\/p>\n<p>The <code>fgets()<\/code> function is generally used to read a single line from the specified file.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$myfile = fopen(&quot;webdictionary.txt&quot;, &quot;r&quot;) or die(&quot;Unable to open file!&quot;);<br \/>\necho fgets($myfile);<br \/>\nfclose($myfile);<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the above code, we have used the echo statement with <code>fgets()<\/code> function. The function is telling php script to read the single line from the variable name <code>$myfile<\/code> which is equal to the fopen file name webdictionary.txt. so as per instruction the function reads the file and shows the first line available in the file.<\/p>\n<p>The above code shows result.<\/p>\n<p><code>AJAX = Asynchronous JavaScript and XML<\/code><\/p>\n<p><strong>feof() Function<\/strong><\/p>\n<p>The <code>feof()<\/code> function checks if the end of the file has been reached. It is used to check whether the pointer has reached the end of the file.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$myfile = fopen(&quot;webdictionary.txt&quot;, &quot;r&quot;) or die(&quot;Unable to open file!&quot;);<br \/>\n\/\/ Output one line until end-of-file<br \/>\nwhile(!feof($myfile)) {<br \/>\n  echo fgets($myfile) . &quot;&lt;br&gt;&quot;;<br \/>\n}<br \/>\nfclose($myfile);<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>We have used while function to check the end of file has been reached or not. It loops the statement one by one on a single line.<\/p>\n<p>The above code will show the result.<\/p>\n<p><code>AJAX = Asynchronous JavaScript and XML<br \/>\nCSS = Cascading Style Sheets<br \/>\nHTML = Hyper Text Markup Language<br \/>\nPHP = PHP Hypertext Preprocessor<br \/>\nSQL = Structured Query Language<br \/>\nSVG = Scalable Vector Graphics<br \/>\nXML = EXtensible Markup Language<\/code><\/p>\n<p><strong>fgetc() function<\/strong><\/p>\n<p>The <code>fgetc()<\/code> function is used to read a single character from the file one by one.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$myfile = fopen(&quot;webdictionary.txt&quot;, &quot;r&quot;) or die(&quot;Unable to open file!&quot;);<br \/>\n\/\/ Output one character until end-of-file<br \/>\nwhile(!feof($myfile)) {<br \/>\n  echo fgetc($myfile);<br \/>\n}<br \/>\nfclose($myfile);<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the above code when we use <code>fgetc()<\/code> we are telling php script to check each character from the file name webdictionary.txt.<\/p>\n<p><code>fopen()<\/code> function to create a file<\/p>\n<p>The <code>fopen()<\/code> function is used to create a new file if it doesn&#8217;t exist in the folder.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p><code>$myfile = fopen(\"testfile.txt\", \"w\")<\/code><\/p>\n<p><strong>Code Explanation;<\/strong><\/p>\n<p>The <code>fopon()<\/code> function is used to create a file testfile.txt. It will create a file in the same folder.<\/p>\n<p><strong>fwrite() function<\/strong><\/p>\n<p>The php <code>fwrite()<\/code> is used to write a text into the file. <\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$myfile = fopen(&quot;newfile.txt&quot;, &quot;w&quot;) or die(&quot;Unable to open file!&quot;);<br \/>\n$txt = &quot;John Doe\\n&quot;;<br \/>\nfwrite($myfile, $txt);<br \/>\n$txt = &quot;Jane Doe\\n&quot;;<br \/>\nfwrite($myfile, $txt);<br \/>\nfclose($myfile);<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the above code, we have use <code>fwrite()<\/code> function to write the text <code>$txt<\/code> in the file <code>$myfile<\/code> which equal to newfile.txt. We have mentioned the fwrite function twice to write the two separate entries. Once executed we get the following result.<\/p>\n<p><code>John Doe<br \/>\nJane Doe<\/code><\/p>\n<p>Both the names are written in the file name newfile.txt file.<\/p>\n<p><strong>File Upload<\/strong><\/p>\n<p><code>php.ini<\/code><\/p>\n<p>First, you need check whether your server allowing the file to uploaded. Check the php.ini file on your server and edit it to find the below line.<\/p>\n<p><code>file_uploads = On<\/code><\/p>\n<p>If it says off then change it to ON and save the file again.<\/p>\n<p><strong>Upload File PHP Script<\/strong><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$target_dir = &quot;uploads\/&quot;;<br \/>\n$target_file = $target_dir . basename($_FILES[&quot;fileToUpload&quot;][&quot;name&quot;]);<br \/>\n$uploadOk = 1;<br \/>\n$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));<br \/>\n\/\/ Check if image file is a actual image or fake image<br \/>\nif(isset($_POST[&quot;submit&quot;])) {<br \/>\n    $check = getimagesize($_FILES[&quot;fileToUpload&quot;][&quot;tmp_name&quot;]);<br \/>\n    if($check !== false) {<br \/>\n        echo &quot;File is an image &#8211; &quot; . $check[&quot;mime&quot;] . &quot;.&quot;;<br \/>\n        $uploadOk = 1;<br \/>\n    } else {<br \/>\n        echo &quot;File is not an image.&quot;;<br \/>\n        $uploadOk = 0;<br \/>\n    }<br \/>\n}<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p><code>$target_dir = \"uploads\/\"<\/code>: The variable specifies the directory name where you will be uploading the file.<\/p>\n<p><code>$target_file<\/code> : It specifies the path of the file in the directory.<\/p>\n<p><code>$imageFileType<\/code>: This variable holds the file extension in the lower case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>File handling is used often to read and write the particular file. It is an important part of the web application. In php, you can<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"predecessor-version":[{"id":360,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/359\/revisions\/360"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}