0

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:

  1. 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.

  2. I want to output a more detailed message if mkdir fails, to tell the user what he/she can do to correct the problem.

codefactor
  • 1,616
  • 2
  • 18
  • 41
  • 2
    "How can I validate $album_name?" What exactly do you mean by "validate"? What have you tried? – Patrick Q Jan 10 '14 at 21:21
  • I don't know if this is exactly what your talking about, but I did a Google search and found a function that indicates whether or not a file/directory exists: http://us3.php.net/file_exists – donsiuch Jan 10 '14 at 21:22
  • I have modified the post with a clarification. – codefactor Jan 10 '14 at 21:31
  • @donsiuch My code includes the `file_exists` function call already. I am more worried about invalid characters in the directory name. – codefactor Jan 10 '14 at 21:53

1 Answers1

1

You can use a regex or just check to see if the mkdir was successful.

See the answers on this questions for reference: Check a string for a valid directory name

Edit 1:

To check if the mkdir function will succeed you can first use php functions such as is_writable to check if you have permission to write in that location. If not you can let your user know (but if thats the case you should probably be considering why they are trying to write somewhere they dont have permission). Otherwise if the mk_dir function fails you will be able to log the failure and check the issue later. Other than that you probably shouldnt tell the user much without first investigating the problem

Community
  • 1
  • 1
RyanS
  • 3,964
  • 3
  • 23
  • 37