I would like to create a function that will create a directory to store photos in the file system. Suppose there is a directory called "albums" in the root directory. There is a form on the page that allows the user to create a new album, which will create a subdirectory under "albums".
Also I want the ability to have nested photo albums. When creating a new album, the admin can provide the album name with the forward slash (/
) separators to recursively create this folder structure.
I am using directories because I want the ability to manage the photo albums via direct FTP access to the server for dealing with massive photo albums. Each photo directory can have an optional album.properties
file to have any meta data associated with the album itself (such as a thumbnail to use for the album).
if (!is_admin()) die();
$album_name = $_GET['album_name'];
if ($album_name) {
$directory = "/albums/" . $album_name;
// TODO: How can I validate $album_name?
if (file_exists($directory)) {
echo "Album already exists, please choose a different name.";
} else if (mkdir($album_name), 0777, true) { // Recursive is true
echo "Album successfully created.";
} else {
// TODO: Is there a way to output a more detailed explanation?
echo "The album could not be created.";
}
}
See the TODO
markers in the PHP code. Also if you have any advice regarding my approach so far, that would be useful.
Note that it was only recently that I decided to use directories and property files instead of database tables to store meta data associated with the albums/photos because I only recently found out that the client wished to use FTP.
I will still need to have database tables, though, to allow the admin to assign privileges to already existing albums. For example, the admin can share a particular album with the public or other non-admin users. The /albums
directory will not be directly accessible, instead another .php
will allow download of photos or read meta data, but only to permissioned users.
Edit:
Adding more clarification on what I want:
To validate that the
$album_name
is a valid directory name, if it is not valid I want to output an error message. Also I would like to explain to the user how to choose a valid directory name.I want to output a more detailed message if mkdir fails, to tell the user what he/she can do to correct the problem.