PHP Script to Create Links to Each Subdirectory Using Page Titles as Anchor Text

I had a thought today. I have numerous, changing folders in part of my website, and I wanted to create a page that automatically generates links to each of those folders (or subpages) which I could then style and use, with the title attributes of the pages contained within those folders as the anchor text. So I did some Google searching as a starting point (I’m no PHP expert.) I finally came up with a script that would generate a page accomplishing exactly what we want.

Here is the script. Just save this in a file called “index.php” in the folder containing all the subfolders you want to link to.  Then replace the “” with the URL of the directory whose subfolders you want to link to. I even included a command in there that will prevent the script from returning links to folders that return a 403 error. This prevents folders without webpages on them from being listed. You could remove that part if you don’t want it in there. You can also change the page title to whatever you want, it’s currently “Page Links.”

I originally thought of this, because I have a folder full of temporary websites that I’m working on, and I got tired of typing out the full path every single time while I was working on them. This way, it automatically generates the links and then I can just click the one I want to go to! I also included as part of this script a link to style.css which would be your stylesheet. Just create that file in the same directory, and then you can format your links however you want.

Credit to SN from for the script to name the subfolders as a starting point, and further credits to shamittomar at for the script to strip the title from a webpage.

6 Comments on “PHP Script to Create Links to Each Subdirectory Using Page Titles as Anchor Text”

  1. Thank you for sharing.
    What happens if the folder I am to add index.php already has another index.php.

    1. You can just name it something else. You would just, then, need to navigate to the correct address.

      For example, if you name it example.php and put it in folder “test”, you’ll need to navigate to

  2. Hello Brian. I was looking the web for a script like yours. I’m just starting out to learn about html/css, and want to learn php later on. My knowledge about php is limited, so I tried your script.
    When I implement it on my site, the links works great, but the title is showing “404 page not found”. If I understand it correctly, they should show the title element from each index while in the subfolders? Looking forwaord to your reply – either here or on mail.

    1. Yes title comes from the title tag within the file of that page. Whatever page you were on has that as the title. Is it the page for 404? Does it bring you to the correct location?

Leave a Reply

Your email address will not be published. Required fields are marked *