Php-mysql Course

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:
<?php
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:
<?php
$dir = './files';

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

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

// read the dir-content with readdir()
if ($dc = @opendir($dir)) {
  while($ok = readdir($dc)) {
    echo '<br />'. $ok;
  }
  closedir($dc);
}
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()
.
..
image.jpg
tempdir
test.txt

• 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:
<?php
$path = 'files/php-mysql/file_system.php';
$ar_path = pathinfo($path);
var_export($ar_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

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Exploring Folders

Last accessed pages

  1. String Object (667)
  2. Register and show online users and visitors (39428)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (26057)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137889)
  5. Adding text with ActionScript 3 (5501)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (202)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide