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 type of <input> creates input fields that should contain a numeric value?
type="text" type="number" type="date"
<input type="number" name="points" min="5" max="80" />
Which CSS property allows to change the transparency of an element?
font-style opacity color
#id {
  filter:alpha(opacity=40);    /* for IE */
Click on the function that formats a number to specified number of characters.
toPrecision() exp() toFixed()
var num = 12.34567;
alert( num.toPrecision(3) );       // 12.3
Which function randomizes the order of the elements into an array?
natsort() shuffle() sort()
$lang =[10=>"PHP", 20=>"JavaScript", "site"=>"");
var_export($lang);     // array (0=>"", 1=>"PHP", 2=>"JavaScript")
Exploring Folders

Last accessed pages

  1. New Form elements and attributes in HTML5 (4513)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (102518)
  3. Contact page - CoursesWeb (41020)
  4. Tabs effect with CSS (41631)
  5. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (48803)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (721)
  2. Contact page - CoursesWeb (694)
  3. Tabs effect with CSS (692)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (286)
  5. Insert, Select and Update NULL value in MySQL (254)