{"id":184,"date":"2018-07-12T11:51:42","date_gmt":"2018-07-12T11:51:42","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=184"},"modified":"2018-12-07T11:03:55","modified_gmt":"2018-12-07T11:03:55","slug":"file-handling-i","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/file-handling-i\/","title":{"rendered":"File Handling \u2013 I"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/xH_6VOfE6OM\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The data which is used by most web sites is stored in a database. Though, in some cases some of the data might be stored in files. Data can be used to read from files and write to files by using various built in function in PHP.<\/p>\r\n&nbsp;\r\n\r\n<strong>Functions to determine file or directory exists<\/strong>\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 161px\" border=\"1\" width=\"624\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 129.063px\"><strong>Function<\/strong><\/td>\r\n<td style=\"width: 466.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\">is_file($path)<\/td>\r\n<td style=\"width: 466.063px\">Returns true if $path is a file otherwise returns false.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\">is_dir($path)<\/td>\r\n<td style=\"width: 466.063px\">Returns true if $path is a directory otherwise returns false.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\">file_exists($path)<\/td>\r\n<td style=\"width: 466.063px\">Returns true if $path exists and $path is either file or directory.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p style=\"text-align: center\"><strong>A constant that contains the correct path separator<\/strong><\/p>\r\n\r\n<table class=\"aligncenter\" style=\"height: 89px\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 33px\">\r\n<td style=\"height: 33px;width: 175.063px\"><strong>Constant<\/strong><\/td>\r\n<td style=\"height: 33px;width: 477.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 175.063px\" rowspan=\"2\">DIRECTORY_SEPARATOR\r\n\r\n&nbsp;<\/td>\r\n<td style=\"height: 56px;width: 477.063px\" rowspan=\"2\">A directory separator\u00a0 irrespective of operating system. Backward slash\r\n\r\nfor windows OS and forward slash for Linux or MAC OS<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nConsider the following code. We have save this file as 1. is_file.php.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$path = 'H:\\wamp\\www\\PathshalaWAD\\File Handling - I';\r\n\r\n$file = $path.DIRECTORY_SEPARATOR.'1. is_file.php';\r\n\r\n$dir = $path.DIRECTORY_SEPARATOR.'New Folder';\r\n\r\necho \"File is $file\";\r\n\r\n$a = is_file($file);\r\n\r\nif($a === TRUE)\r\n\r\n{\r\n\r\necho \"&lt;br&gt; $file exists\";\r\n\r\n}\r\n\r\nelse\r\n\r\n{\r\n\r\necho \"&lt;br&gt; $file does not\u00a0 exist\";\r\n\r\n}\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0 DIRECTORY $b = is_dir($dir); if($b === TRUE)\r\n\r\n{\r\n\r\necho \"&lt;br&gt; $dir is directory\";\r\n\r\n}\r\n\r\nelse\r\n\r\n{\r\n\r\necho \"&lt;br&gt; $dir is not a directory\";\r\n\r\n}\r\n\r\n\/\/\u00a0 file_exists\r\n\r\n$c = file_exists($file);\r\n\r\nif($c === true)\r\n\r\n{\r\n\r\necho \"&lt;br&gt; $file exists\";\r\n\r\n}\r\n\r\nelse\r\n\r\n{\r\n\r\necho \"&lt;br&gt; $file does not exist\";\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThe output\u00a0 will be:\r\n\r\n&nbsp;\r\n\r\nFile is H:\\wamp\\www\\PathshalaWAD\\File Handling - I\\1. is_file.php\r\n\r\nH:\\wamp\\www\\PathshalaWAD\\File Handling - I\\1. is_file.php exists\r\n\r\nH:\\wamp\\www\\PathshalaWAD\\File Handling - I\\New Folder is directory\r\n\r\nH:\\wamp\\www\\PathshalaWAD\\File Handling - I\\1. is_file.php exists\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: left\"><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Function to get a directory listing<\/strong><\/p>\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 122px\" border=\"1\" width=\"710\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 108.063px\"><strong>Function<\/strong><\/td>\r\n<td style=\"width: 573.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 108.063px\" rowspan=\"2\">scandir($path)<\/td>\r\n<td style=\"width: 573.063px\" rowspan=\"2\">Returns an array having a list of files and directory in $path, if $path is a valid\r\ndirectory name. Otherwise it return false.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nFollowing code shows the use of scandir function.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$cwd = getcwd();\r\n\r\n$dir = scandir($cwd);\r\n\r\nforeach($dir as $d)\r\n\r\n{\r\n\r\necho \"&lt;br&gt; $d\";\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThe output\u00a0 will be\r\n\r\n1.\u00a0 is_file.php\r\n\r\n2.\u00a0 scandir.php\r\n\r\nNew Folder\r\n\r\nNew folder (2)\r\n\r\n&nbsp;\r\n\r\nIn case, we are interested to display only files within a directory, the code can be used as follow:\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$cwd = getcwd();\r\n\r\n$dir = scandir($cwd);\r\n\r\n\/\/only file\r\n\r\necho \"&lt;br&gt; To display only files\";\r\n\r\n$file = array();\r\n\r\nforeach($dir as $d)\r\n\r\n{\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(is_file($d))<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$file[] = $d;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">foreach($file as $f)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; $f\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output is:\r\n\r\n&nbsp;\r\n\r\nTo display only files\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 is_file.php\r\n\r\n2.\u00a0 scandir.php\r\n\r\n3.\u00a0 scandir shows only file.php\r\n\r\n&nbsp;\r\n\r\n<strong>Read and Write an entire file<\/strong>\r\n\r\n&nbsp;\r\n\r\nFunctions to read an entire file\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Function<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>readfile($name)<\/td>\r\n<td>Reads the file $name and echoed it to the web page<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>file_get_contents ($name)<\/td>\r\n<td>Returns the string having content of a file $name<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">file($name)<\/td>\r\n<td rowspan=\"2\">Returns an array which contains each line as one element of array\r\nfor file $name<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider there is a file \u2018city.txt\u2019 having some name of the cities. Following code shows example using file function. In the same manner, you can use file_get_contents and readfile function.<\/p>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$cwd = getcwd();\r\n\r\n$sep = DIRECTORY_SEPARATOR;\r\n\r\n$filename = 'city.txt';\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$file = $cwd.$sep.$filename;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$f = file($file);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">foreach($f as $line)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"$line &lt;br&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output is:\r\n\r\n&nbsp;\r\n\r\nAhmedabad\r\n\r\nRajkot\r\n\r\nSurat\r\n\r\nBaroda\r\n\r\nBhuj\r\n\r\n&nbsp;\r\n\r\n<strong>Functions to write an entire file<\/strong>\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 93px\" border=\"1\" width=\"626\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 255.063px\"><strong>Function<\/strong><\/td>\r\n<td style=\"width: 342.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 255.063px\">file_put_contents ($name, $data)<\/td>\r\n<td style=\"width: 342.063px\">Write the data string $data to the file $name.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$cwd = getcwd();\r\n\r\n$sep = DIRECTORY_SEPARATOR;\r\n\r\n$filename = 'city.txt';\r\n\r\n$file = $cwd.$sep.$filename;\r\n\r\n$newcity = 'Patan';\r\n\r\nfile_put_contents($file,$newcity);\r\n\r\necho \"&lt;br&gt; New City added successfully to the file city.txt\"; ?&gt;\r\n\r\n&nbsp;\r\n\r\nThen it will overwrite the old content and add $data to the file $file.\r\n\r\n&nbsp;\r\n\r\n<strong>Write an array to a file<\/strong>\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 &lt;?php<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$cwd = getcwd();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$sep = DIRECTORY_SEPARATOR;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$filename = 'names.txt';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$file = $cwd.$sep.$filename;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$names = array('Juhi','Neelam','Minakshee');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$names = implode(\"\\n\",$names);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">file_put_contents($file,$names);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe above code will overwrite the file names.txt with $names.\r\n\r\n&nbsp;\r\n\r\n<strong>Read a file into an array<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$cwd = getcwd();\r\n\r\n$sep = DIRECTORY_SEPARATOR;\r\n\r\n$filename = 'names.txt';\r\n\r\n$file = $cwd.$sep.$filename;\r\n\r\n$names = file($file);\r\n\r\nforeach($names as $n)\r\n\r\n{\r\n\r\necho \"$n &lt;br&gt;\";\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\n<strong>Read and Write part of a file<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The functions described earlier is used to read or write an entire file. These functions are helpful when file size is small. When the file size is large, generally it is preferred to read or write a portion of the file. When you use file for read or write, it consumes internal memory. So read and write a portion of a file use less internal memory. To read or write a portion of the file, following functions are used.<\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Functions to open and close the file<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<table class=\"aligncenter\" style=\"height: 126px\" border=\"1\" width=\"602\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 153.063px\"><strong>Function<\/strong><\/td>\r\n<td style=\"width: 420.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 153.063px\">fopen($path,$mode)<\/td>\r\n<td style=\"width: 420.063px\">Opens the $path file in $mode and returns a file handle<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 153.063px\">feof($file)<\/td>\r\n<td style=\"width: 420.063px\">Returns true when End Of File for $file is reached.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 153.063px\">fclose($file)<\/td>\r\n<td style=\"width: 420.063px\">Closes the $file<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">To open a file, it is compulsory to specify how you want to open the file. There are various modes to open a file. The below table describes various file open modes.<\/p>\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Mode<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>r<\/td>\r\n<td>Open the file for reading. It starts reading from the beginning of the file.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>r+<\/td>\r\n<td>Open the file for reading and writing. It starts reading from the beginning of the file.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"3\">w<\/td>\r\n<td rowspan=\"3\">Open the file for writing. It starts writing from the beginning of the file. In case the\r\nfile already exists, this mode delete the existing content.\u00a0 If file does not exist, try to\r\ncreate it.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"3\">w+<\/td>\r\n<td rowspan=\"3\">Open the file for writing and reading. It starts writing or reading from the beginning of\r\nthe file. In case the file already exists, this mode delete the existing content.\u00a0 If file\r\ndoes not exist, try to create it.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">a<\/td>\r\n<td rowspan=\"2\">Open the file for appending (writing) only. It starts writing from the ending of the\r\nexisting content, if any. If file does not exist, try to create it.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">a+<\/td>\r\n<td rowspan=\"2\">Open the file for appending (writing) and reading. It starts writing or reading from the\r\nending of the existing content, if any. If file does not exist, try to create it.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"3\">x<\/td>\r\n<td rowspan=\"3\">Open the file for writing. It starts writing from the beginning of the file. In case the\r\nfile already exists, this mode will not open the file. fopen function will return false and\r\nPHP displays warning.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"3\">x+<\/td>\r\n<td rowspan=\"3\">Open the file for writing and reading. It starts writing\/reading from the beginning of\r\nthe file. In case the file already exists, this mode will not open the file. fopen function\r\nwill return false and PHP displays warning.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"4\">b<\/td>\r\n<td rowspan=\"4\">Used in conjunction with all of the above mode (r,r+,w,w+,a,a+,x,x+). This mode is\r\ngenerally used if file system differentiates between binary and text files. Windows OS\r\ndifferentiate file system while Unix OS does not differentiate. PHP developers\r\nrecommended to use it to gain maximum portability. This is the default mode.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">t<\/td>\r\n<td rowspan=\"2\">Used in conjunction with all of the above mode (r,r+,w,w+,a,a+,x,x+). This mode\r\noption is used with Windows OS only.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Functions which read from the file and write to the file<\/strong><\/p>\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Function<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>fread($file , $length)<\/td>\r\n<td>Reads up to $length bytes from $file<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>fgets($file)<\/td>\r\n<td>Reads a line from $file<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>fwrite($file , $data)<\/td>\r\n<td>Writes the string content $data to the file $file<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nThe following code shows how to read 10 bytes of file names.txt using fread function.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0$cwd = getcwd();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$sep = DIRECTORY_SEPARATOR;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$name = 'names.txt';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$filename = $cwd.$sep.$name;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$file = fopen($filename,'rb');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$l =fread($file,10);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; $l\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fclose($file);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>The output is:<\/strong>\r\n\r\n&nbsp;\r\n\r\nJuhi Neela\r\n\r\nFollowing code shows read line by line of a file using fgets function.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$cwd = getcwd();\r\n\r\n$sep = DIRECTORY_SEPARATOR;\r\n\r\n$name = 'names.txt';\r\n\r\n$filename = $cwd.$sep.$name;\r\n\r\n$file = fopen($filename,'rb');\r\n\r\nwhile(!feof($file))\r\n\r\n{\r\n\r\n$line = fgets($file);\r\n\r\necho \"$line &lt;br&gt;\";\r\n\r\n}\r\n\r\nfclose($file);\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 Juhi\r\n\r\nNeelam\r\n\r\nMinakshee\r\n\r\n&nbsp;\r\n\r\nFollowing code shows how to write into a file using fwrite\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$cwd = getcwd();\r\n\r\n$dir = scandir($cwd);\r\n\r\n$file = fopen('dir.txt','ab');\r\n\r\nforeach($dir as $d)\r\n\r\n{\r\n\r\nif(is_dir($d))\r\n\r\n{\r\n\r\nfwrite($file,$d.\"\\n\");\r\n\r\n}\r\n\r\n}\r\n\r\nfclose($file);\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nAbove code will create a new file dir.txt and add the directory name in the dir.txt file.\r\n\r\n&nbsp;\r\n\r\n<strong>Locking Files<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When a file is used by more than one user to write simultaneously, it can be corrupted. If one person write into the file and the same time other person open the same file to read it, this situation can get odd result. The reason is that it depends on operating system. To handle simultaneous user, the built-in file locking function flock is used. Flock function make queues of the request to access the file until the lock is released. So, when you code for writing to a file, you should use flock function. Flock can be used for reading a file.<\/p>\r\n&nbsp;\r\n\r\nThe syntax of flock is:\r\n\r\nflock($file, lock_mode)\r\n\r\n$file is a file-pointer and lock_mode is operative value of the lock.\r\n\r\nflock returns true if lock is acquired successfully otherwise false.\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\nThe following table shows flock operation values.\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Value of Operation<\/strong><\/td>\r\n<td><strong>Meaning<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">LOCK_SH<\/td>\r\n<td rowspan=\"2\">Reading lock. In reading lock mode file can be shared to read file with other\r\nusers.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">LOCK_EX<\/td>\r\n<td rowspan=\"2\">Exclusive lock. It is also called writing lock. In this mode, file is not shared with\r\nother user.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>LOCK_UN<\/td>\r\n<td>Unlock. This mode is unlocked the existing lock.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>LOCK_NB<\/td>\r\n<td>Block lock. This mode prevents blocking while trying to acquire a lock.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nFlock function is called after fopen function and used before writing to a file.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Note that flock is not worked with NFS or other networked file systems. Flock also not worked with older file system such as FAT. In some operating system, flock is implement at process level and it is not worked correctly with multithread server API. Following code segment show how to use flock function.<\/p>\r\n&nbsp;\r\n\r\n$cwd = getcwd();\r\n\r\n$dir = scandir($cwd);\r\n\r\n$file = fopen('dir.txt','ab');\r\n\r\nflock($file, LOCK_EX);\r\n\r\n$new = \u2018ABC\u2019;\r\n\r\nfwrite($file,$new);\r\n\r\nflock($file,LOCK_UN);\r\n\r\nfclose($file);\r\n\r\n&nbsp;\r\n\r\nThough, flock is used to solve simultaneous writing into a file, it is not a full-proof solution.\r\n\r\n&nbsp;\r\n\r\nRDBMS is the preferred way to handling simultaneous writing in database.\r\n\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on File Handling \u2013 I<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/xH_6VOfE6OM\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<strong>References:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP, Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. www.php.net<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/xH_6VOfE6OM\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The data which is used by most web sites is stored in a database. Though, in some cases some of the data might be stored in files. Data can be used to read from files and write to files by using various built in function in PHP.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Functions to determine file or directory exists<\/strong><\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 161px; width: 624px;\">\n<tbody>\n<tr>\n<td style=\"width: 129.063px\"><strong>Function<\/strong><\/td>\n<td style=\"width: 466.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\">is_file($path)<\/td>\n<td style=\"width: 466.063px\">Returns true if $path is a file otherwise returns false.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\">is_dir($path)<\/td>\n<td style=\"width: 466.063px\">Returns true if $path is a directory otherwise returns false.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\">file_exists($path)<\/td>\n<td style=\"width: 466.063px\">Returns true if $path exists and $path is either file or directory.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: center\"><strong>A constant that contains the correct path separator<\/strong><\/p>\n<table class=\"aligncenter\" style=\"height: 89px\">\n<tbody>\n<tr style=\"height: 33px\">\n<td style=\"height: 33px;width: 175.063px\"><strong>Constant<\/strong><\/td>\n<td style=\"height: 33px;width: 477.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 175.063px\" rowspan=\"2\">DIRECTORY_SEPARATOR<\/p>\n<p>&nbsp;<\/td>\n<td style=\"height: 56px;width: 477.063px\" rowspan=\"2\">A directory separator\u00a0 irrespective of operating system. Backward slash<\/p>\n<p>for windows OS and forward slash for Linux or MAC OS<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Consider the following code. We have save this file as 1. is_file.php.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$path = &#8216;H:\\wamp\\www\\PathshalaWAD\\File Handling &#8211; I&#8217;;<\/p>\n<p>$file = $path.DIRECTORY_SEPARATOR.&#8217;1. is_file.php&#8217;;<\/p>\n<p>$dir = $path.DIRECTORY_SEPARATOR.&#8217;New Folder&#8217;;<\/p>\n<p>echo &#8220;File is $file&#8221;;<\/p>\n<p>$a = is_file($file);<\/p>\n<p>if($a === TRUE)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; $file exists&#8221;;<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; $file does not\u00a0 exist&#8221;;<\/p>\n<p>}<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 DIRECTORY $b = is_dir($dir); if($b === TRUE)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; $dir is directory&#8221;;<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; $dir is not a directory&#8221;;<\/p>\n<p>}<\/p>\n<p>\/\/\u00a0 file_exists<\/p>\n<p>$c = file_exists($file);<\/p>\n<p>if($c === true)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; $file exists&#8221;;<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; $file does not exist&#8221;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The output\u00a0 will be:<\/p>\n<p>&nbsp;<\/p>\n<p>File is H:\\wamp\\www\\PathshalaWAD\\File Handling &#8211; I\\1. is_file.php<\/p>\n<p>H:\\wamp\\www\\PathshalaWAD\\File Handling &#8211; I\\1. is_file.php exists<\/p>\n<p>H:\\wamp\\www\\PathshalaWAD\\File Handling &#8211; I\\New Folder is directory<\/p>\n<p>H:\\wamp\\www\\PathshalaWAD\\File Handling &#8211; I\\1. is_file.php exists<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: left\"><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Function to get a directory listing<\/strong><\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 122px; width: 710px;\">\n<tbody>\n<tr>\n<td style=\"width: 108.063px\"><strong>Function<\/strong><\/td>\n<td style=\"width: 573.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 108.063px\" rowspan=\"2\">scandir($path)<\/td>\n<td style=\"width: 573.063px\" rowspan=\"2\">Returns an array having a list of files and directory in $path, if $path is a valid<br \/>\ndirectory name. Otherwise it return false.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Following code shows the use of scandir function.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$cwd = getcwd();<\/p>\n<p>$dir = scandir($cwd);<\/p>\n<p>foreach($dir as $d)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; $d&#8221;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output\u00a0 will be<\/p>\n<p>1.\u00a0 is_file.php<\/p>\n<p>2.\u00a0 scandir.php<\/p>\n<p>New Folder<\/p>\n<p>New folder (2)<\/p>\n<p>&nbsp;<\/p>\n<p>In case, we are interested to display only files within a directory, the code can be used as follow:<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$cwd = getcwd();<\/p>\n<p>$dir = scandir($cwd);<\/p>\n<p>\/\/only file<\/p>\n<p>echo &#8220;&lt;br&gt; To display only files&#8221;;<\/p>\n<p>$file = array();<\/p>\n<p>foreach($dir as $d)<\/p>\n<p>{<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(is_file($d))<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$file[] = $d;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">foreach($file as $f)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; $f&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The output is:<\/p>\n<p>&nbsp;<\/p>\n<p>To display only files<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 is_file.php<\/p>\n<p>2.\u00a0 scandir.php<\/p>\n<p>3.\u00a0 scandir shows only file.php<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Read and Write an entire file<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Functions to read an entire file<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Function<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>readfile($name)<\/td>\n<td>Reads the file $name and echoed it to the web page<\/td>\n<\/tr>\n<tr>\n<td>file_get_contents ($name)<\/td>\n<td>Returns the string having content of a file $name<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">file($name)<\/td>\n<td rowspan=\"2\">Returns an array which contains each line as one element of array<br \/>\nfor file $name<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider there is a file \u2018city.txt\u2019 having some name of the cities. Following code shows example using file function. In the same manner, you can use file_get_contents and readfile function.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$cwd = getcwd();<\/p>\n<p>$sep = DIRECTORY_SEPARATOR;<\/p>\n<p>$filename = &#8216;city.txt&#8217;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$file = $cwd.$sep.$filename;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$f = file($file);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">foreach($f as $line)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;$line &lt;br&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The output is:<\/p>\n<p>&nbsp;<\/p>\n<p>Ahmedabad<\/p>\n<p>Rajkot<\/p>\n<p>Surat<\/p>\n<p>Baroda<\/p>\n<p>Bhuj<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Functions to write an entire file<\/strong><\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 93px; width: 626px;\">\n<tbody>\n<tr>\n<td style=\"width: 255.063px\"><strong>Function<\/strong><\/td>\n<td style=\"width: 342.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 255.063px\">file_put_contents ($name, $data)<\/td>\n<td style=\"width: 342.063px\">Write the data string $data to the file $name.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$cwd = getcwd();<\/p>\n<p>$sep = DIRECTORY_SEPARATOR;<\/p>\n<p>$filename = &#8216;city.txt&#8217;;<\/p>\n<p>$file = $cwd.$sep.$filename;<\/p>\n<p>$newcity = &#8216;Patan&#8217;;<\/p>\n<p>file_put_contents($file,$newcity);<\/p>\n<p>echo &#8220;&lt;br&gt; New City added successfully to the file city.txt&#8221;; ?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Then it will overwrite the old content and add $data to the file $file.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Write an array to a file<\/strong><\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 &lt;?php<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$cwd = getcwd();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$sep = DIRECTORY_SEPARATOR;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$filename = &#8216;names.txt&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$file = $cwd.$sep.$filename;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$names = array(&#8216;Juhi&#8217;,&#8217;Neelam&#8217;,&#8217;Minakshee&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$names = implode(&#8220;\\n&#8221;,$names);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">file_put_contents($file,$names);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>The above code will overwrite the file names.txt with $names.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Read a file into an array<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$cwd = getcwd();<\/p>\n<p>$sep = DIRECTORY_SEPARATOR;<\/p>\n<p>$filename = &#8216;names.txt&#8217;;<\/p>\n<p>$file = $cwd.$sep.$filename;<\/p>\n<p>$names = file($file);<\/p>\n<p>foreach($names as $n)<\/p>\n<p>{<\/p>\n<p>echo &#8220;$n &lt;br&gt;&#8221;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Read and Write part of a file<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The functions described earlier is used to read or write an entire file. These functions are helpful when file size is small. When the file size is large, generally it is preferred to read or write a portion of the file. When you use file for read or write, it consumes internal memory. So read and write a portion of a file use less internal memory. To read or write a portion of the file, following functions are used.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Functions to open and close the file<\/span><\/p>\n<\/div>\n<div>\n<table class=\"aligncenter\" style=\"height: 126px; width: 602px;\">\n<tbody>\n<tr>\n<td style=\"width: 153.063px\"><strong>Function<\/strong><\/td>\n<td style=\"width: 420.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 153.063px\">fopen($path,$mode)<\/td>\n<td style=\"width: 420.063px\">Opens the $path file in $mode and returns a file handle<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 153.063px\">feof($file)<\/td>\n<td style=\"width: 420.063px\">Returns true when End Of File for $file is reached.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 153.063px\">fclose($file)<\/td>\n<td style=\"width: 420.063px\">Closes the $file<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To open a file, it is compulsory to specify how you want to open the file. There are various modes to open a file. The below table describes various file open modes.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Mode<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>r<\/td>\n<td>Open the file for reading. It starts reading from the beginning of the file.<\/td>\n<\/tr>\n<tr>\n<td>r+<\/td>\n<td>Open the file for reading and writing. It starts reading from the beginning of the file.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"3\">w<\/td>\n<td rowspan=\"3\">Open the file for writing. It starts writing from the beginning of the file. In case the<br \/>\nfile already exists, this mode delete the existing content.\u00a0 If file does not exist, try to<br \/>\ncreate it.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"3\">w+<\/td>\n<td rowspan=\"3\">Open the file for writing and reading. It starts writing or reading from the beginning of<br \/>\nthe file. In case the file already exists, this mode delete the existing content.\u00a0 If file<br \/>\ndoes not exist, try to create it.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">a<\/td>\n<td rowspan=\"2\">Open the file for appending (writing) only. It starts writing from the ending of the<br \/>\nexisting content, if any. If file does not exist, try to create it.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">a+<\/td>\n<td rowspan=\"2\">Open the file for appending (writing) and reading. It starts writing or reading from the<br \/>\nending of the existing content, if any. If file does not exist, try to create it.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"3\">x<\/td>\n<td rowspan=\"3\">Open the file for writing. It starts writing from the beginning of the file. In case the<br \/>\nfile already exists, this mode will not open the file. fopen function will return false and<br \/>\nPHP displays warning.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"3\">x+<\/td>\n<td rowspan=\"3\">Open the file for writing and reading. It starts writing\/reading from the beginning of<br \/>\nthe file. In case the file already exists, this mode will not open the file. fopen function<br \/>\nwill return false and PHP displays warning.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"4\">b<\/td>\n<td rowspan=\"4\">Used in conjunction with all of the above mode (r,r+,w,w+,a,a+,x,x+). This mode is<br \/>\ngenerally used if file system differentiates between binary and text files. Windows OS<br \/>\ndifferentiate file system while Unix OS does not differentiate. PHP developers<br \/>\nrecommended to use it to gain maximum portability. This is the default mode.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">t<\/td>\n<td rowspan=\"2\">Used in conjunction with all of the above mode (r,r+,w,w+,a,a+,x,x+). This mode<br \/>\noption is used with Windows OS only.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Functions which read from the file and write to the file<\/strong><\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Function<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>fread($file , $length)<\/td>\n<td>Reads up to $length bytes from $file<\/td>\n<\/tr>\n<tr>\n<td>fgets($file)<\/td>\n<td>Reads a line from $file<\/td>\n<\/tr>\n<tr>\n<td>fwrite($file , $data)<\/td>\n<td>Writes the string content $data to the file $file<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The following code shows how to read 10 bytes of file names.txt using fread function.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0$cwd = getcwd();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$sep = DIRECTORY_SEPARATOR;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$name = &#8216;names.txt&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$filename = $cwd.$sep.$name;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$file = fopen($filename,&#8217;rb&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$l =fread($file,10);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; $l&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fclose($file);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>The output is:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Juhi Neela<\/p>\n<p>Following code shows read line by line of a file using fgets function.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$cwd = getcwd();<\/p>\n<p>$sep = DIRECTORY_SEPARATOR;<\/p>\n<p>$name = &#8216;names.txt&#8217;;<\/p>\n<p>$filename = $cwd.$sep.$name;<\/p>\n<p>$file = fopen($filename,&#8217;rb&#8217;);<\/p>\n<p>while(!feof($file))<\/p>\n<p>{<\/p>\n<p>$line = fgets($file);<\/p>\n<p>echo &#8220;$line &lt;br&gt;&#8221;;<\/p>\n<p>}<\/p>\n<p>fclose($file);<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 Juhi<\/p>\n<p>Neelam<\/p>\n<p>Minakshee<\/p>\n<p>&nbsp;<\/p>\n<p>Following code shows how to write into a file using fwrite<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$cwd = getcwd();<\/p>\n<p>$dir = scandir($cwd);<\/p>\n<p>$file = fopen(&#8216;dir.txt&#8217;,&#8217;ab&#8217;);<\/p>\n<p>foreach($dir as $d)<\/p>\n<p>{<\/p>\n<p>if(is_dir($d))<\/p>\n<p>{<\/p>\n<p>fwrite($file,$d.&#8221;\\n&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>fclose($file);<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Above code will create a new file dir.txt and add the directory name in the dir.txt file.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Locking Files<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When a file is used by more than one user to write simultaneously, it can be corrupted. If one person write into the file and the same time other person open the same file to read it, this situation can get odd result. The reason is that it depends on operating system. To handle simultaneous user, the built-in file locking function flock is used. Flock function make queues of the request to access the file until the lock is released. So, when you code for writing to a file, you should use flock function. Flock can be used for reading a file.<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax of flock is:<\/p>\n<p>flock($file, lock_mode)<\/p>\n<p>$file is a file-pointer and lock_mode is operative value of the lock.<\/p>\n<p>flock returns true if lock is acquired successfully otherwise false.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The following table shows flock operation values.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Value of Operation<\/strong><\/td>\n<td><strong>Meaning<\/strong><\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">LOCK_SH<\/td>\n<td rowspan=\"2\">Reading lock. In reading lock mode file can be shared to read file with other<br \/>\nusers.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">LOCK_EX<\/td>\n<td rowspan=\"2\">Exclusive lock. It is also called writing lock. In this mode, file is not shared with<br \/>\nother user.<\/td>\n<\/tr>\n<tr>\n<td>LOCK_UN<\/td>\n<td>Unlock. This mode is unlocked the existing lock.<\/td>\n<\/tr>\n<tr>\n<td>LOCK_NB<\/td>\n<td>Block lock. This mode prevents blocking while trying to acquire a lock.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Flock function is called after fopen function and used before writing to a file.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Note that flock is not worked with NFS or other networked file systems. Flock also not worked with older file system such as FAT. In some operating system, flock is implement at process level and it is not worked correctly with multithread server API. Following code segment show how to use flock function.<\/p>\n<p>&nbsp;<\/p>\n<p>$cwd = getcwd();<\/p>\n<p>$dir = scandir($cwd);<\/p>\n<p>$file = fopen(&#8216;dir.txt&#8217;,&#8217;ab&#8217;);<\/p>\n<p>flock($file, LOCK_EX);<\/p>\n<p>$new = \u2018ABC\u2019;<\/p>\n<p>fwrite($file,$new);<\/p>\n<p>flock($file,LOCK_UN);<\/p>\n<p>fclose($file);<\/p>\n<p>&nbsp;<\/p>\n<p>Though, flock is used to solve simultaneous writing into a file, it is not a full-proof solution.<\/p>\n<p>&nbsp;<\/p>\n<p>RDBMS is the preferred way to handling simultaneous writing in database.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on File Handling \u2013 I<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/xH_6VOfE6OM\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>References:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP, Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\n<p style=\"text-align: justify\">11. www.php.net<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n","protected":false},"author":3,"menu_order":17,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-184","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/184\/revisions"}],"predecessor-version":[{"id":498,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/184\/revisions\/498"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/184\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=184"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=184"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=184"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}