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:
- scandir('dir_path') - Returns an array of files and directories inside the "dir_path" (sorted in alphabetical ascending order), or FALSE on failure.
- readdir($dir_handle) - Returns the filename of the next file from the folder, or False. The filenames are returned in the order in which they are stored by the filesystem.
- "$dir_handle" is a handle resource opened with opendir()
-
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):
- dirname - parent directory's path. The same as dirname() function
- basename - the base name of the given path. The same as basename() function
- extension (if any) - the extension of the base name (if exists)
- filename - the name of file, without extension
-
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
- getcwd() - Gets the current working directory
- parse_url($url) - parses a URL and returns an associative array containing the fallowing items (keys), with components of the URL:
- "scheme" (e.g. http), "host", "port", "user", "pass", "path", "query" (after the question mark ?), "fragment" (after the hashmark #)
- Example:
<?php
$url = 'https://coursesweb.net/php-mysql/index.php?id=89#mp';
$ar_parse_url = parse_url($url);
var_export($ar_parse_url);
/* Output:
array ( 'scheme' => 'http', 'host' => 'coursesweb.net', 'path' => '/php-mysql/index.php', 'query' => 'id=89', 'fragment' => 'mp', )
*/
?>
- disk_free_space($dir) - returns the free space, in bytes, of the specified directory
- is_dir('dir') - returns TRUE if the "dir" exists
- is_file('file') - returns TRUE if "file" is a regular file
- is_readable('filename') - returns TRUE if the file or directory specified by "filename" exists and is readable
- is_writable('filename') - returns TRUE if the file or directory specified by "filename" exists and is writable