PHP has functions that can also open directories (folders) and explore their contents, or create new folders.

Creating folders with PHP

To create a folder with PHP, use the mkdir() function. Returns TRUE on success or FALSE on failure.
  - Syntax:
mkdir('pathname ', mode)
- "pathname " is the path and name of the new directory.
- "mode" sets the CHMOD permissions. It is optional (0777 by defaul).
  - Example:
if (mkdir('files/newdir', 0755)) echo 'The "newdir" was created';
else 'Unable to create "newdir"';
This code attempt to create a folder named "newdir" in the "files" directory, with 0755 CHMOD permissions, and output a message on success or failure.
PHP must have write permissions in the "files" directory.

Reading a directory content

You can read the content of a directory with one of the fallowing functions:
  - Example:
$dir = './files';

// read the dir-content with scandir()
$ar_dir = scandir($dir);

echo '<h5>Using readdir()</h5>';

// read the dir-content with readdir()
if ($dc = @opendir($dir)) {
  while($ok = readdir($dc)) {
    echo '<br />'. $ok;
else echo 'Unable to open the directory';
"./" indicates the root directory of the Web site (www or htdocs). So, the "files" folder is located in the Web site root folder.
"opendir()" opens a directory and return a reference to it; "closedir()" closes an opened folder (similar with "fopen()" and "fclose()" for files).
The above code will output something similar to:
array ( 0 => '.', 1 => '..', 2 => 'image.jpg', 3 => 'tempdir', 4 => 'test.txt', )
Using readdir()

• Another way to Inspect the contents of a folder is with the DirectoryIterator class.
It returns an SplFileInfo object that gives you access to a lot more information about the folder's contents. See the official reference: The DirectoryIterator class

Get information about a file path

An useful PHP function to get info about a file path is pathinfo(). It returns an associative Array with the fallowing elements (keys):
  - Example:
$path = 'files/php-mysql/file_system.php';
$ar_path = pathinfo($path);

echo '<hr />';

// or, using directly dirname() and basename() function
echo '<br /> dirname() returns - '. dirname($path);
echo '<br /> basename() returns - '. basename($path);
This code will output:
array ( 'dirname' => 'files/php-mysql', 'basename' => 'file_system.php', 'extension' => 'php', 'filename' => 'file_system', )
dirname() returns - files/php-mysql
basename() returns - file_system.php

Other useful functions for file (folder) system

Daily Test with Code Example

Which tag adds an image in web page?
<div> <img> <span>
<img src="" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
Exploring Folders

Last accessed pages

  1. Butterfly Kyodai (13670)
  2. Chicago Car Parking (328)
  3. Zoo Amigos (3245)
  4. Remove / Get duplicate array values - Reset array keys in PHP (6403)
  5. Bubble Tanks 3 (20217)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (50849)
  2. Read Excel file data in PHP - PhpExcelReader (39684)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (37892)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (34163)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (31636)