PHP Classes

PHP Path Name: Manipulate paths of files and directories

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 144 All time: 9,155 This week: 488Up
Version License PHP version Categories
path-name 1.0Public Domain5.3Files and Folders
Description 

Author

This class can manipulate paths of files and directories.

It can take a file name and uses the pathinfor function parse it into its parts like the base file name, name extension, directory parts, etc..

The class defines a setter function to alter just some parts of the file name.

It also returns the reassembled path when its value is retrieved as a string.


Simplifies the modification of a pathname, e.g. by changing file extension, directory name etc. Internally uses and "wraps" the functionality of the built-in pathinfo() function and presents it in what is hoped to be more memorable and intuitive.

Picture of CPK Smithies
  Performance   Level  
Name: CPK Smithies <contact>
Classes: 7 packages by
Country: United Kingdom
Age: 70
All time rank: 151869 in United Kingdom
Week rank: 206 Up12 in United Kingdom Up
Innovation award
Innovation award
Nominee: 4x

Documentation

Contents of this package

path.php contains class file\path. This class facilitates the manipulation of filenames; effectively it is a wrapper of PHP's built-in pathinfo() function.

modifier.php contains an example class that uses file\path.

phpdoc.dist.xml contains configuration information to generate automatic documentation using the phpDocumentor PEAR package.

tests/path_test.php contains some tests that can be run using phpunit:

$ phpunit tests/path_test.php

This test file also gives some examples of how the file\path class can be used.

Using file\path

The aim of this class is to make filename manipulation easy and intuitive.

Example 1: To rename a file in the current directory from foo.txt to foo.bak.

<?php require 'path.php';

$filename = 'foo.txt'; $backupfilename = new file\path($filename); $backupfilename->extension = 'bak'; rename($filename, $backupfilename); ?>

Example 2: to move a file to the system temporary directory:

<?php require 'path.php'; $filename = 'foo.txt'; $destination = new file\path($filename); $destination->set_temp_dir(); rename($filename, $destination); ?>

Example 3: to move a file to the current directory:

<?php require 'path.php';

$filename = new file\path('/var/www/foo.txt'); if ($filename->exists()) { $destination = new file\path($filename); $destination->set_current_dir(); rename($filename, $destination); } ?>

In order to remove part of the pathname, e.g. file extension or directory, simply assign NULL to that part:

<?php require 'path.php';

$filename = new file\path('/var/www/foo.txt'); $filename->dirname = $filename->extension = NULL; echo "$filename\n"; // outputs "foo\n" ?>

You can also build a pathname from scratch like this:

<?php require 'path.php';

$filename = new file\path; $filename->basename = 'foo.txt'; $filename->dir = '/var/www'; echo "$filename\n"; // outputs "/var/www/foo.txt" ?>


  Files folder image Files (6)  
File Role Description
Files folder imagetests (1 file)
Accessible without login Plain text file LICENCE Lic. Licence text
Plain text file modifier.php Class Class demonstrating use of file\path
Plain text file path.php Class file\path class source
Accessible without login Plain text file phpdoc.dist.xml Data Configuration for phpDocumentor
Accessible without login Plain text file README Doc. Documentation text

  Files folder image Files (6)  /  tests  
File Role Description
  Accessible without login Plain text file path_test.php Test Unit tests for file\path

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:144
This week:0
All time:9,155
This week:488Up