FileExplorer
By: Sitebase
Email: wim@sitebase.be
Table of Contents
A. Description
If you already worked with files and directories in PHP then you will know that the default copy, delete(unlink) functions are ok to use on files but when you want to delete or copy a directory with content in it, it's an whole other story. This class gives you the ability to easily delete, create, move, copy files and directories with content.
B. Features
- Copy files and folders
- Move files and folders
- Delete files and folders
- Create files and folders
- Read and write a file
- Listing the content of a directory
- Get file extension
C. Install script
To use this class, you only need to include it in your PHP code.
You do this like:
D. Methods
Below you find a list of methods and how you can use them.
Select Path
With the setpath method you can select a path to a directory or file where you want to do an action on.
$File = new Explorer();
$File->SetPath('c:/test/example.txt');
Or if you want to do an action on a specific directory:
$File = new Explorer();
$File->SetPath('c:/test/');
Write to a file
If you want to write content to a file.
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/example.txt');
$File->Write('hello world');
The content of the example.txt file will be "hello world" after you execute this script.
Read from a file
With the read method you can get the content from a file.
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/example.txt');
$content = $File->Read();
echo $content;
Create directory or file
With this method you can create directories and files. The advantage of this method is that it also creates the parent directories if they doesn't exist.
This method takes 2 parameters. The first parameters must be set to true if you want to create a directory. Default this parameter is set to false. The second parameter can be set to true if you want to overwrite a file that already exists. Default this parameter is also false.
For example if we want to create a directory c:/test/sitebase/project. But there's also no directory sitebase under test. Then this method will automaticly create the sitebase and project directory.
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/sitebase/project');
$File->Create(true);
The true must be set because we want to create a directory in this case.
If you want to create a file it will look something like this.
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/sitebase/project/description.txt');
$File->Create();
Delete
This deletes directories and files. It very easy to use. You select a directory or file with SetPath and then you call the Delete method.
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/sitebase');
$File->Delete();
If it's a directory then automaticly all the content will also be deleted.
Copy
Copy a directory or a file. This method has one parameter and that's the directory or filename to where you want to copy the file or directory.
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/sitebase');
$File->Copy("c:/test/sitebase_backup");
Move
Move a directory or a file. This method has one parameter and that's the directory or filename to where you want to move the file or directory to.
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/sitebase');
$File->Move("c:/test/sitebase_new");
You can also use this move function as directory an file renamer.
Listing
If a directory is selected this method returns a list of files and directories that are in this directory. This method has 4 optitional parameters:
- $exclude_extension=array(): Array of extension that you want to exclude from the result. For example array("jpg")
- $exclude_file=array(): Array of files that you want to exclude from the result. For example array("Thumb.db")
- $exclude_dir=array(): Array of directories you want to exclude from the result. For example array("backup", "temp")
- $recursive=true: Set to true if you also want to list the content of subdirectories
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/sitebase');
$array = $File->Listing();
print_r($array);
This is an example of an array that is returned from the listing method:
(
[0] => Array
(
[extension] => php
[type] => file
[path] => C:/xampp/htdocs/Themeforest/File/
[filename] => File.php
[fullpath] => C:/xampp/htdocs/Themeforest/File/File.php
)
[1] => Array
(
[extension] => html
[type] => file
[path] => C:/xampp/htdocs/Themeforest/File/
[filename] => help.html
[fullpath] => C:/xampp/htdocs/Themeforest/File/help.html
)
[2] => Array
(
[extension] => jpg
[type] => file
[path] => C:/xampp/htdocs/Themeforest/File/
[filename] => preview.jpg
[fullpath] => C:/xampp/htdocs/Themeforest/File/preview.jpg
)
[3] => Array
(
[extension] => psd
[type] => file
[path] => C:/xampp/htdocs/Themeforest/File/
[filename] => preview.psd
[fullpath] => C:/xampp/htdocs/Themeforest/File/preview.psd
)
[4] => Array
(
[extension] => php
[type] => file
[path] => C:/xampp/htdocs/Themeforest/File/
[filename] => test.php
[fullpath] => C:/xampp/htdocs/Themeforest/File/test.php
)
[5] => Array
(
[extension] => jpg
[type] => file
[path] => C:/xampp/htdocs/Themeforest/File/
[filename] => thumb.jpg
[fullpath] => C:/xampp/htdocs/Themeforest/File/thumb.jpg
)
[6] => Array
(
[extension] => psd
[type] => file
[path] => C:/xampp/htdocs/Themeforest/File/
[filename] => thumb.psd
[fullpath] => C:/xampp/htdocs/Themeforest/File/thumb.psd
)
)
As you can see it contains much more info than only the filename.
Extension
This gets the extension is a file is selected.
include("File.php");
$File = new Explorer();
$File->SetPath('c:/test/description.txt');
$extension = $File->Extension();
E. Method return
The methods write, copy, delete and move return a true if the action is finished with succes or else they return false.