Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Script Tips - Working with Directoris and Files

By: FYICenter.com

Part:   1  2  

(Continued from previous part...)

How To Dump the Contents of a Directory into an Array?

If you want to get the contents of a directory into an array, you can use the scandir() function. It gets a list of all the files and sub directories of the specified directory and returns the list as an array. The returning list also includes two specify entries: (.) and (..). Here is a PHP script example on how to use scandir():

<?php
mkdir("/temp/download");
$array["one"] = "Download PHP scripts at dev.fyicenter.com.\r\n";
$array["two"] = "Download Perl scripts at dev.fyicenter.com.\r\n";
$bytes = file_put_contents("/temp/download/todo.txt", $array);
$files = scandir("/temp/download");
print_r($files);
?>

This script will print:

Array
(
    [0] => .
    [1] => ..
    [2] => todo.txt
)

How To Read a Directory One Entry at a Time?

If you want to read a directory one entry at a time, you can use opendir() to open the specified directory to create a directory handle, then use readdir() to read the directory contents through the directory handle one entry at a time. readdir() returns the current entry located by the directory pointer and moves the pointer to the next entry. When end of directory is reached, readdir() returns Boolean false. Here is a PHP script example on how to use opendir() and readdir():

<?php
mkdir("/temp/download");
$array["one"] = "Download PHP scripts at dev.fyicenter.com.\r\n";
$array["two"] = "Download Perl scripts at dev.fyicenter.com.\r\n";
$bytes = file_put_contents("/temp/download/todo.txt", $array);
print("List of files:\n");
$dir = opendir("/temp/download");
while ( ($file=readdir($dir)) !== false ) {
  print("$file\n");
}
closedir($dir);
?>

This script will print:

List of files:
.
..
todo.txt

How To Get the Directory Name out of a File Path Name?

If you have the full path name of a file, and want to get the directory name portion of the path name, you can use the dirname() function. It breaks the full path name at the last directory path delimiter (/) or (\), and returns the first portion as the directory name. Here is a PHP script example on how to use dirname():

<?php
$pathName = "/temp/download/todo.txt";
$dirName = dirname($pathName);
print("File full path name: $pathName\n");
print("File directory name: $dirName\n");
print("\n");
?>

This script will print:

File full path name: /temp/download/todo.txt
File directory name: /temp/download

How To Break a File Path Name into Parts?

If you have a file name, and want to get different parts of the file name, you can use the pathinfo() function. It breaks the file name into 3 parts: directory name, file base name and file extension; and returns them in an array. Here is a PHP script example on how to use pathinfo():

<?php
$pathName = "/temp/download/todo.txt";
$parts = pathinfo($pathName);
print_r($parts);
print("\n");
?>

This script will print:

Array
(
    [dirname] => /temp/download
    [basename] => todo.txt
    [extension] => txt
)

Part:   1  2  


Selected Developer Jobs:

More...