{"id":2238,"date":"2015-04-25T14:54:47","date_gmt":"2015-04-25T21:54:47","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2238"},"modified":"2015-09-30T14:51:54","modified_gmt":"2015-09-30T21:51:54","slug":"find-all-the-files-in-a-directory","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2238","title":{"rendered":"Find all the files in a directory."},"content":{"rendered":"<p>When I find a comic amusing, I grab a screenshot of it. I thought it would be nice to display one comic on my home page each day, so I wrote a little code to look in the comics directory and find all the files, then randomly choose one to display.<\/p>\n<p>There are a few interesting things about the code. First up is scope. Notice that I initialized the two arrays outside of the while loop. That way when I add elements to the arrays, they are not local to the loop. Second, notice that I don\u2019t use an index to add to the arrays. In PHP the preferred way to add an element to an array is with the $arrayName[] notation. Third, I define $location to be the directory handle of the directory. Since I assign it in an if statement, if it fails to find the directory, none of the rest of the code runs. Fourth, I use a similar pattern to read the files in the directory. I read files until there aren\u2019t any more. Finally, I use a flag in my URL so I can proof all of the comics\u2014 $all  = $_GET[&#8216;all&#8217;];. <\/p>\n<p><pre><code class=\"\u2019smallest\u2019 preserve-code-formatting\">\n&lt;?php\n\n$title = array();\n$comicsLst = array();\n\n$title = array();\n$comicsLst = array();\n\nif ($location = opendir(&#039;.\/Comics&#039;)) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;while (false !== ($entry = readdir($location))) {\n&nbsp;&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($entry != &quot;.&quot; &amp;&amp; $entry != &quot;..&quot;) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$titleArray = explode(&quot;.&quot;, $entry);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$title[] = $titleArray[0];\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$comicsLst[] = $entry;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;closedir($location);\n&nbsp;&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;$numComics = count($comicsLst);\n\n&nbsp;&nbsp;&nbsp;&nbsp;$randomComic = rand(0,$numComics);\n\n&nbsp;&nbsp;&nbsp;&nbsp;$all&nbsp;&nbsp;= $_GET[&#039;all&#039;];\n\n&nbsp;&nbsp;&nbsp;&nbsp;if (is_null($all)) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;p&gt;&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;img class=&#039;align-left&#039; src=&#039;\/Comics\/$comicsLst[$randomComic]&#039; &quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;alt=&#039;$title[$randomComic]&#039; title=&#039;$title[$randomComic]&#039; \/&gt; &quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;\/p&gt;&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;p class=&#039;attribution&#039;&gt;&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;a href=&#039;index.php?p=Comics&amp;all=y&#039;&gt;Display all comics.&lt;\/a&gt;&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;\/p&gt;&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;} else {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for($i=0; $i&lt;$numComics; $i++) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;p&gt;&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;$title[$i]&lt;br \/&gt;&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;img class=&#039;align-left&#039; src=&#039;\/Comics\/$comicsLst[$i]&#039; alt=&#039;$title[$i]&#039; \/&gt; &quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;\/p&gt;&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n\n<\/code><\/pre><\/p>\n<p>I frequently find new comics and add them to my local folder. To keep them synchronized with the server, I wrote a little rsync script.<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n#!\/bin\/bash\nrsync -a -H -vv -z -l --update --delete --exclude \\ Comics --exclude .DS_Store ~wellgolly\/Documents\/Comics\/ \\\nwellgolly@ wellgolly:\/www\/WellGolly\/Comics\/ &gt; \\\n~ wellgolly\/Sites\/rsync-backup-comics-`date +%F`.log\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I find a comic amusing, I grab a screenshot of it. I thought it would be nice to display one comic on my home page each day, so I wrote a little code to look in the comics directory and find all the files, then randomly choose one to display. There are a few &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2238\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Find all the files in a directory.<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2238","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2238"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2238\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}