I’ve got quite the collection of movies, scattered across several external hard drives, and I wanted a better way to keep track of where everything is stored. It’s like having my personal digital library, but without a proper catalog. So, I rolled up my sleeves and set out to create a simple PHP script to help me take notes of what I store and where.
The Challenge
The mission was clear: create a PHP script to list all the directories across my external hard drives where my beloved movies are tucked away. No files, just the folders, since I’m after the big picture here.
The PHP Magic
PHP is pretty fantastic for file system tasks, and with a few lines of code, I crafted a script that did exactly what I needed. Here’s the secret sauce to my organizational success:
<?php
// Function to list all folders in a directory
function listFolders($dir){
$ffs = scandir($dir); // Get files and folders
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..' && is_dir($dir.'/'.$ff)){ // Check if it's a directory
echo '<li>'.$ff;
listFolders($dir.'/'.$ff); // Recursively list subfolders
echo '</li>';
}
}
echo '</ol>';
}
// Starting with the directory where my hard drives are mounted
listFolders('/media/my-external-hdds');
?>
How It Saved My Movie Nights
With this script, I was able to get an organized view of my movie collection. No more guessing which hard drive contains the « Lord of the Rings » trilogy or where I stashed « Inception ». It’s all there, clear as day, in a neatly formatted list.
A Few Things I Learned Along the Way
- Tailor to Fit: I added some HTML and CSS to make the output more visually appealing and match the aesthetics of my home media setup.
- Efficiency is Key: When dealing with multiple hard drives, especially with large collections, optimizing the script’s performance can save a lot of waiting time.
This is a more complete script, in a more eye-friendly suit, by using bootstrap to view the files.
<?php
// Function to read all files and folders and display them grouped by their folder
function listFolderFiles($dir){
$ffs = scandir($dir); // Get files and folders
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li>'.$ff;
if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff); // If it's a folder, call recursively
echo '</li>';
}
}
echo '</ol>';
}
// Function to read all folders and display them grouped by their parent folder
function listFolders($dir){
$ffs = scandir($dir); // Get files and folders
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..' && is_dir($dir.'/'.$ff)){ // Check if it's a directory
echo '<li>'.$ff;
listFolders($dir.'/'.$ff); // Call recursively for nested folders
echo '</li>';
}
}
echo '</ol>';
}
$p = filter_input(INPUT_POST, 'p');
?><!DOCTYPE html>
<html>
<head>
<title>List</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="Demo project">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<style type="text/css"></style>
</head>
<body>
<div class="container">
<form action="" method="POST" class="form-horizontal" role="form">
<div class="form-group">
<label for="textarea" class="col-sm-2 control-label"> </label>
<div class="col-sm-10">
<input type="text" name="p" class="form-control" required="required" value="<?php echo $p ?>">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<button type="submit" class="btn btn-default">Analyse</button>
</div>
</div>
</form><?php
if ( $p ):
listFolders( $p );
endif;
?></div>
</body>
</html>