PHP Classes
elePHPant
Icontem

PHP File Folder: Manipulate directories, files and their contents

Recommend this page to a friend!
  Info   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-02-18 (14 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 220 This week: 1All time: 7,984 This week: 405Up
Version License PHP version Categories
filefolder 1.1.0MIT/X Consortium ...7PHP 5, Files and Folders, Traits
Description Author

This package can manipulate directories, files and their contents.

There is a class that can perform several types of operations with files like:

- Check if a file exists with a given path
- Get the file size, base name, directory, the file name extension and MIME type, owner user and group, access permissions,
- Get the file size in a human readable format
- Check if it is readable or writable
- Create, rename and delete files
- Open, read, write, append and close files
- Change the file access offset position

Another class that can also perform several types of operations with folders like:

- Check if a folder exists with a givem path
- Create a new folder
- Check the write access permissions
- Traverse a folder recursively and retrieve a tree of files it contains
- Change the access permissions
- Delete a directory
- Etc

  Performance   Level  
Name: Laudir Bispo <contact>
Classes: 5 packages by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 1x

Details

File and Folder handler

Installation

Install the latest version with

$ composer require laudirbispo/filefolder

Basic Usage

<?php

use laudirbispo\FileAndFolder\{File, Folder};

 ### File class example
$File = new File('/site/index.php');
var_dump($File->exists());     
// return bool true

var_dump($File->extension());
// return string 'php' (length=3)

var_dump($File->dirname());
// return string 'C:/wamp64/www/emobi/site' (length=24)

var_dump($File->basename());
// return string 'index.php' (length=9)

var_dump($File->name());
// return string 'index' (length=5)

var_dump($File->size());
// return int 1432

var_dump($File->humanSize());
// return string '1,4 KB' (length=6)

var_dump($File->mimeType());
// return string 'text/x-php' (length=10)

var_dump($File->isValidMime());
// return boolean true

var_dump($File->isExecutable());
// return boolean false

var_dump($File->isReadable());
// return boolean true

var_dump($File->isWritable());
// return boolean true

var_dump($File->group());
// return Gets the file group. The group ID is returned in numerical format, use posix_getgrgid() to resolve it to a group name. 

var_dump($File->lastAccess());
// return int 1550083697

var_dump($File->lastChange());
// return int 1550083697

var_dump($File->owner());
// returns the user ID of the owner of the file, or FALSE on failure. The user ID is returned in numerical format, use posix_getpwuid() to resolve it to a username. 

var_dump($File->permissions());
// return octal chmod string '0666'

var_dump($File->setChmod(0755));
// return boolean true

var_dump($File->getInfo());
/ return array 
  'dirname' => string 'C:/wamp64/www/emobi/site' (length=24)
  'basename' => string 'index.php' (length=9)
  'extension' => string 'php' (length=3)
  'filename' => string 'index' (length=5)
  'mime_type' => string 'text/x-php' (length=10)
  'size' => int 1432
  'human_size' => string '1,4 KB' (length=6)
  'permissions' => string '0666' (length=4)
  'owner' => int 0
  'group' => int 0
  'last_access' => int 1550083697
*/

var_dump($File->setFile('other_file'));

var_dump($File->delete());
// return boolean true
 
 

Creating a new file

<?php

use libs\laudirbispo\FileAndFolder\{File, Folder};

$txtContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras pulvinar nec elit in feugiat.  \n";
$textContent2 = "Etiam congue turpis sed blandit pulvinar.";

$File = new File('new_file.txt');

var_dump($File->create(0644));
// return boolean true

var_dump($File->open());
// return boolean true

$data = $File->read();
var_dump($data);
// return string ''

$txtContent = $File->prepare($txtContent, true);
// Prepares an ASCII string for writing. Converts line endings to the
// correct terminator for the current platform. If Windows, "\r\n" will be used,
// all other platforms will use "\n"

var_dump($File->write($txtContent));
// add content

var_dump($File->append($textContent2));
// adds content to the end of the pointer

$data = $File->read();
var_dump($data);
// return string 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras pulvinar nec elit in feugiat.  
// Etiam congue turpis sed blandit pulvinar.' (length=136)

var_dump($File->replaceText('Lorem ipsum dolor sit amet', '...replace text'));
// return boolean true

$data = $File->read();
var_dump($data);
// return string '...replace text, consectetur adipiscing elit. Cras pulvinar nec elit in feugiat.  
// Etiam congue turpis sed blandit pulvinar.' (length=125)

var_dump($File->clear());
// cleans the contents of the file

var_dump($File->close());
// close the file


Working with folders example

<?php

use libs\laudirbispo\FileAndFolder\{File, Folder};

use libs\laudirbispo\FileAndFolder\{File, Folder};

$Folder = new Folder('/teste');
var_dump($Folder->create());
// Create a new folder

var_dump($Folder->tree());
// Get an array containing the list of directories and files present

var_dump($Folder->setChmod(0755));
// return boolean

var_dump($Folder->exists());
// return boolean 

var_dump($Folder->isWritable());
// return boolean 

var_dump($Folder->isWritable());
// return boolean 

var_dump($Folder->delete());
// return boolean 

var_dump($Folder->hasErrors());
// return boolean 

var_dump($Folder->getErrors());
// return array

Author

Laudir Bispo - <laudirbispo@outlook.com> - <https://twitter.com/laudir_bispo><br />

License

FileAndFolder is licensed under the MIT License - see the LICENSE file for details

  Files folder image Files  
File Role Description
Files folder imagesrc (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageFileAndFolder (5 files)

  Files folder image Files  /  src  /  FileAndFolder  
File Role Description
  Plain text file File.php Class Class source
  Plain text file Folder.php Class Class source
  Accessible without login Plain text file LICENSE.txt Lic. License
  Accessible without login Plain text file mime_types.php Conf. Configuration script
  Plain text file TraitFileAndFolder.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:220
This week:1
All time:7,984
This week:405Up