{"id":504,"date":"2020-04-16T00:32:00","date_gmt":"2020-04-16T04:32:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=504"},"modified":"2021-01-02T00:35:49","modified_gmt":"2021-01-02T05:35:49","slug":"php-file-functions","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/php-file-functions\/","title":{"rendered":"PHP: File functions"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Opening directory and listing files<\/h3>\n\n\n\n<p>See the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$dir = 'mydir';\n$myfiles = scandir($dir);\nprint_r($myfiles);<\/code><\/pre>\n\n\n\n<p>output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array\n(\n  &#91;0] => .\n  &#91;1] => ..\n  &#91;2] => one.txt\n  &#91;3] => two.txt\n)<\/code><\/pre>\n\n\n\n<p>To remove certain file name from the list, see the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$dir = 'mydir';\n$aExcept = array('.', '..');\n$myfiles = array_diff(scandir($dir), $aExcept);\nprint_r($myfiles);<\/code><\/pre>\n\n\n\n<p>This code lists all files except the ones listed in the $aExcept array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array\n(\n  &#91;2] => one.txt\n  &#91;3] => two.txt\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Getting filesize of a file<\/h3>\n\n\n\n<p>filesize function returns size of a file in bytes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$f = 'files\/debug.log';\necho filesize2bytes(filesize($f));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Deleting file contents<\/h3>\n\n\n\n<p>Deleting file contents To delete file contents and not the file, open the file in write mode and close is without writing anything.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$fh = fopen(\"file.txt\", 'w');\nfclose($fh);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Copying and renaming files<\/h3>\n\n\n\n<p>To copy files, use the copy() function. To rename files, use the rename() function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$oldfile = 'file.txt';\n$newfile = 'file2.txt';\ncopy($oldfile, $newfile);\nrename($newfile,'file3.txt');<\/code><\/pre>\n\n\n\n<p>copy() will create a new file if it does not exist. If it exists, copy() would delete its contents.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Writing to file<\/h3>\n\n\n\n<p>The following code writes a string to a file called out.txt inside the data directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$f = \"data\/out.txt\";\n$fh = fopen($f, 'w') or die(\"can't open file\");\n$sData = \"Some text\\nmore text\";\nfwrite($fh, $sData);\nfclose($fh);<\/code><\/pre>\n\n\n\n<p>Make sure that Apache has write permissions to the data directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Counting lines in a text file<\/h3>\n\n\n\n<p>The following simple code count the number of lines in a text file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print count(file('file.txt'));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reading file contents to an array and string<\/h3>\n\n\n\n<p>PHP allows you to easily read your file contents into an array or a string. To read into a string, use file_get_contents() function. To read into an array, use file() function. The file function would copy each line of content into a different array element.<\/p>\n\n\n\n<p><strong>Grocery List<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bread\neggs\nmilk<\/code><\/pre>\n\n\n\n<p>grocery_list.txt<\/p>\n\n\n\n<p><strong>PHP code<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$afile = file('grocery_list.txt');\n$sfile = file_get_contents('grocery_list.txt');\nprint '&lt;br>file() returns contents of file as ' . gettype($afile);\nprint '&lt;br>file_get_contents() returns contents of file as ' . gettype($sfile);\nprint '&lt;br>';\nprint_r($afile);\nprint '&lt;br>';\nprint_r($sfile);<\/code><\/pre>\n\n\n\n<p>grocery_list.php<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file() returns contents of file as array\nfile_get_contents() returns contents of file as string\nArray ( &#91;0] => bread &#91;1] => eggs &#91;2] => milk )\nbread eggs milk <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reading from file<\/h3>\n\n\n\n<p>Following is a code snippet which reads from a dta file. A dta file is an ms\/ms data file format. It contains numbers separated by spaces. Something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1764.9410 2\n120.0740 30.7619\n136.0696 37.0476\n175.1109 40.6191<\/code><\/pre>\n\n\n\n<p>To read from this file, we can use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$readfile = file(file.dta);\n\n\/\/ read the first line and separate by space\n$fields = split(\" \",$readfile&#91;0]);\nprint \"$fields&#91;0]m$fields&#91;1]&lt;hr>\";\n\n\/\/ read from the second to the last line, \nfor ($i=1; $i &lt;= count($readfile)-1; $i++) {\n  \/\/ separate by space\n  $fields = split(\" \",$readfile&#91;$k]);\n  print \"$fields&#91;0]m$fields&#91;1]&lt;br>\";\n}<\/code><\/pre>\n\n\n\n<p>file() function converts file contents into an array where each line can be accessed by an index (the line number). Next we split the line on some symbol, space in this case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Opening directory and listing files See the following code: output: To remove certain file name from the list, see the following code: This code lists all files except the ones listed in the $aExcept array: Getting filesize of a file filesize function returns size of a file in bytes. Deleting file contents Deleting file contents [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[24],"class_list":["post-504","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=504"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/504\/revisions\/505"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}