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 tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Exploring Folders

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142544)
  2. Laravel - Validation (845)
  3. Vue JS - Transition and Animation (495)
  4. Disable button and Enable it after specified time (17562)
  5. PHP Method Chaining (5464)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (547)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)