PHP File Folder: Manipulate directories, files and their contents

Recommend this page to a friend!
  Info   View files (7)   Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 243 All time: 7,857 This week: 296
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

Picture of Laudir Bispo
  Performance   Level  
Name: Laudir Bispo <contact>
Classes: 7 packages by
Country: Brazil Brazil
Age: 27
All time rank: 2706191 in Brazil Brazil
Week rank: 81 5 in Brazil Brazil
Innovation award
Innovation award
Nominee: 2x

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  
File Role Description
src (1 directory)
composer.json Data Auxiliary data
README.md Data Auxiliary data

  Files  /  src  
File Role Description
FileAndFolder (5 files)

  Files  /  src  /  FileAndFolder  
File Role Description
   File.php Class Class source
   Folder.php Class Class source
   LICENSE.txt Lic. License
   mime_types.php Conf. Configuration script
   TraitFileAndFolder.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:243
This week:0
All time:7,857
This week:296

For more information send a message to info at phpclasses dot org.