PHP code example of alexya-framework / filesystem

1. Go to this page and download the library: Download alexya-framework/filesystem library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexya-framework / filesystem example snippets




if(\Alexya\FileSystem\File::exists("/tmp/test.txt")) {
    $file = new \Alexya\FileSystem\File("/tmp/test.txt");
} else {
    $file = \Alexya\FileSystem\File::make("/tmp/test.txt");
}



$file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);


/**
 * File permissions, the same way as executing `ls -l`
 */
$permissions = "-";

$file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);

if($file->isReadable()) {
    $permissions .= "r";
} else {
    $permissions .= "-";
}
if($file->isWritable()) {
    $permissions .= "w";
} else {
    $permissions .= "-";
}
if($file->isExecutable()) {
    $permissions .= "x";
} else {
    $permissions .= "-";
}



// Static calls
$name      = \Alexya\FileSystem\File::name("/tmp/test.txt");      // $name      = "test"
$extension = \Alexya\FileSystem\File::extension("/tmp/test.txt"); // $extension = "txt"
$basename  = \Alexya\FileSystem\File::basename("/tmp/test.txt");  // $basename  = "test.txt"
$path      = \Alexya\FileSystem\File::path("/tmp/test.txt");      // $path      = "/tmp/test.txt"
$location  = \Alexya\FileSystem\File::location("/tmp/test.txt");  // $location  = "/tmp"

// Object calls
$file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);

$name      = $file->getName();      // $name      = "test"
$extension = $file->getExtension(); // $extension = "txt"
$basename  = $file->getBasename();  // $basename  = "test.txt"
$path      = $file->getPath();      // $path      = "/tmp/test.txt"
$location  = $file->getLocation();  // $location  = "/tmp"

$file->setName("foo");            // File path: /tmp/foo.txt
$file->setExtension("bar");       // File path: /tmp/foo.bar
$file->setBasename("bar.foo");    // File path: /tmp/bar.foo
$file->setPath("/test/test.txt"); // File path: /test/test.txt
$file->setLocation("/home");      // File path: /home/test.txt



$file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);

$file->write("Foo
Bar
test
Test");
$file->append("Bar");

$content = $file->getContent();
/*
$content = "Foo
Bar
test
TestBar"
 */

$firstThreeBytes = $file->read(3, 0);
/*
$firstThreeBytes = "Foo";
 */

$nextThreeBytes = $file->read(3, 3);
/*
$nextThreeBytes = "
Ba"
 */

$between = $file->readBetween(2, 4);
/*
$between = "Bar
test"
 */

$thirdLine = $file->readLine(3);
/*
$thirdLine = "test"
 */

$linesBetween = $file->readLinesBetween(2, 4);
/*
$linesBetween = [
    "Bar",
    "test"
]
 */



if(\Alexya\FileSystem\Directory::exists("/tmp")) {
    $directory = new \Alexya\FileSystem\Directory("/tmp");
} else {
    $directory = \Alexya\FileSystem\Directory::make("/tmp");
}



$directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);


/*
 * Directory /tmp:
 * test     (directory)
 * foo      (directory)
 * bar      (file)
 * test.txt (file)
 */

$directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);

$fileExists = $directory->fileExists("file"); // $fileExists = false
$bar        = $directory->getFile("bar"); // $bar = new \Alexya\FileSystem\File("/tmp/bar")
$files      = $directory->getFiles();
/*
$files = [
    new \Alexya\FileSystem\File("/tmp/bar"),
    new \Alexya\FileSystem\File("/tmp/test.txt")
]
 */



/*
 * Directory /tmp:
 * test     (directory)
 * foo      (directory)
 * bar      (file)
 * test.txt (file)
 */

$directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);

$directoryExists = $directory->directoryExists("dir"); // $directoryExists = false
$test            = $directory->getDirectory("test"); // $test = new \Alexya\FileSystem\Directory("/tmp/test")
$directories     = $directory->getDirectories();
/*
$directories = [
    new \Alexya\FileSystem\Directory("/tmp/test"),
    new \Alexya\FileSystem\Directory("/tmp/foo")
]
 */




// Static calls
$name      = \Alexya\FileSystem\Directory::name("/tmp/test");     // $name      = "test"
$path      = \Alexya\FileSystem\Directory::path("/tmp/test");     // $path      = "/tmp/test"
$location  = \Alexya\FileSystem\Directory::location("/tmp/test"); // $location  = "/tmp"

// Object calls
$file = \Alexya\FileSystem\Directory::make("/tmp/test", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);

$name      = $file->getName();      // $name      = "test"
$path      = $file->getPath();      // $path      = "/tmp/test"
$location  = $file->getLocation();  // $location  = "/tmp"

$file->setName("foo");        // Directory path: /tmp/foo
$file->setPath("/test/test"); // Directory path: /test/test
$file->setLocation("/home");  // Directory path: /home/test