Hi there,
I've search all over the internet on how to list a directory in PHP but failed to do so. I've seen other php scripts but it didn't have the features I want.

Do you have something like this:
1. It allows you to display all the directories and files in a directory.
2. It allows uploading of files in the directory.
3. It allows you to create a directory, or inside the direcotry to make it sub-directory.
4. Finally, It allows you to delete a directory.

Hope I found something like this specification.

Thanks in Advance! :)

Dani AI

Generated

Since you confirmed this is server-side (thanks to for the clarifying question and for confirming), here is a compact, practical blueprint you can drop into a web project. The two immediate priorities are (1) restrict all operations to a single base directory and (2) perform strict validation before any write/delete. That prevents accidental damage or directory-traversal attacks.

A simple, robust directory listing using SPL:

$base = '/var/www/myfiles';
$it = new DirectoryIterator($base);
foreach ($it as $entry) {
    if ($entry->isDot()) continue;
    echo ($entry->isDir() ? '[DIR] ' : '[FILE] ') . htmlspecialchars($entry->getFilename()) . "\n";
}

For uploads, validate size and MIME, use is_uploaded_file() + move_uploaded_file(), and store with a generated filename (or a whitelist of safe names). Keep uploads outside the webroot or disable script execution in the upload folder.

$target = '/var/www/uploads';
if (!empty($_FILES['file'])) {
  if (!is_uploaded_file($_FILES['file']['tmp_name'])) { /* reject */ }
  if ($_FILES['file']['size'] > 5*1024*1024) { /* reject */ }
  $f = new finfo(FILEINFO_MIME_TYPE);
  $mime = $f->file($_FILES['file']['tmp_name']);
  $allowed = ['image/jpeg'=>'jpg','image/png'=>'png','application/pdf'=>'pdf'];
  if (!isset($allowed[$mime])) { /* reject */ }
  $name = uniqid('', true).'.'.$allowed[$mime];
  move_uploaded_file($_FILES['file']['tmp_name'], "$target/$name");
}

Create/delete directories with mkdir($path, 0755, true) and rmdir() for empty dirs; use unlink() for files. Never call a recursive delete without deliberate safeguards (confirmations, admin auth, and path checks). Troubleshoot with permission checks (owner must be webserver user), upload_max_filesize / post_max_size in php.ini, and realpath()/strpos() checks to ensure paths stay under your base directory.

Recommended Answers

All 2 Replies

Just to be clear, when you refer to a directory, are you referring to a directory on the server or a local (PC) directory?

Just to be clear, when you refer to a directory, are you referring to a directory on the server or a local (PC) directory?

Umm, I will use the code in the website, so I say, I'll be using it in a server? if that's what server means. :)

Thank you very much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.