{"id":2279,"date":"2020-12-27T13:24:57","date_gmt":"2020-12-27T12:24:57","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2279"},"modified":"2021-05-28T09:01:37","modified_gmt":"2021-05-28T07:01:37","slug":"php-list-and-link-folders-files","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2279","title":{"rendered":"php list and link folder&#8217;s files"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">an old way<\/h4>\n\n\n\n<p>There is an old way, the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n     $path = \".\/\";\n     $narray=array();\n     $dir_handle = @opendir($path) or die(\"Unable to open $path\");\n     echo \"\";\n     $i=0;\n     while($file = readdir($dir_handle))\n     {\n     if($file != '.' &amp;&amp; $file != '..' &amp;&amp; $file != 'index.php' &amp;&amp; $file != 'normal.inc')\n     {\n     \/\/echo \"&lt;a href='$path\/$file'&gt;$file&lt;\/a&gt;&lt;br\/&gt;\";\n     $narray&#91;$i]=$file;\n     $i++;\n     }\n     }\n     sort($narray);\n\n\t   echo \"&lt;ul&gt;\";\t            \n     for($i=0; $i&lt;sizeof($narray); $i++)\n\t   {\n\t    $filename = str_replace(\".html\", \"\", $narray&#91;$i]) &amp; str_replace(\".php\", \"\", $narray&#91;$i]);\n\t   echo \"&lt;li&gt;&lt;a href='$path$narray&#91;$i]'&gt;$filename&lt;\/a&gt;&lt;\/li&gt;\";\n\t   }\n\t   \n\t   echo \"&lt;\/ul&gt;\";\n\t   \/\/closing the directory\n\t   closedir($dir_handle);\n?&gt;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">a new way: glob<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\t   echo \"&lt;ul>\";\t  \n$phpfiles = glob(\"&#91;^index]*.php\");\nforeach ($phpfiles as $phpfile){\n     echo '&lt;li>&lt;a href=\"'.$phpfile.'\">'.pathinfo($phpfile, PATHINFO_FILENAME).'&lt;\/a>&lt;\/li>'; \n}\t   \n\t   echo \"&lt;\/ul>\";\n?><\/code><\/pre>\n\n\n\n<p>as you can see, the code is much simpler.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>With this code:  <strong>glob(&#8220;[^index]*.php&#8221;)<\/strong>  we have set to list all php files <em>except index.php<\/em>.<\/li><li>with this other:  <strong>&#8216;.pathinfo($phpfile, PATHINFO_FILENAME).&#8217;<\/strong> we have set to show only filenames <em>without extension<\/em><\/li><\/ul>\n\n\n\n<p>of course we could set a subfolder as well, with a code like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$phpfiles = glob(\"&#91;subfolder-name]\/&#91;^index]*.php\");<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>an old way There is an old way, the following: a new way: glob as you can see, the code is much simpler. With this code: glob(&#8220;[^index]*.php&#8221;) we have set to list all php files except index.php. with this other: &#8216;.pathinfo($phpfile, PATHINFO_FILENAME).&#8217; we have set to show only filenames without extension of course we could &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=2279\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;php list and link folder&#8217;s files&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[566],"tags":[615,656,655,654,17],"class_list":["post-2279","post","type-post","status-publish","format-standard","hentry","category-webmaster","tag-automatically","tag-folder-files","tag-link-files","tag-list-files","tag-php"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2279"}],"version-history":[{"count":5,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2279\/revisions"}],"predecessor-version":[{"id":2423,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2279\/revisions\/2423"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}