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 “http://www.urlforyourfolderhere.com/” 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 computing.net for the script to name the subfolders as a starting point, and further credits to shamittomar at stackoverflow.com for the script to strip the title from a webpage.

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

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 yoursite.com/test/example.php.

  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 *