PHP code example of pointybeard / helpers-functions-files

1. Go to this page and download the library: Download pointybeard/helpers-functions-files 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/ */

    

pointybeard / helpers-functions-files example snippets




declare(strict_types=1);
include __DIR__.'/vendor/autoload.php';

use pointybeard\Helpers\Functions\Files;

// Make a test directory and make sure that's where we're working from
Files\realise_directory('test/dest/', Files\FLAG_FORCE);

// Creating a symbolic link lic link again, but this time without ***/
// using Files\FLAG_FORCE
try {
    Files\create_symbolic_link('../README.md');
} catch (Files\Exceptions\Symlink\DestinationExistsException $ex) {
    var_dump('ERROR: '.$ex->getMessage());
}
// string(46) "ERROR: Symbolic link README.md already exists."

/*** Example 3: Attempt to make a link to a file that doesn't exist ***/
try {
    Files\create_symbolic_link('../NOTAFILE');
} catch (Files\Exceptions\Symlink\TargetMissingException $ex) {
    var_dump('ERROR: '.$ex->getMessage());
}
// string(55) "ERROR: Symbolic link target ../NOTAFILE does not exist."

/*** Example 4: Attempt to create a symbolic link where we don't have permissions to ***/
try {
    // Go somewhere we're not supposed to
    chdir('/');
    Files\create_symbolic_link(realpath(__DIR__.'/LICENCE'), 'naughty');
} catch (Files\Exceptions\Symlink\CreationFailedException $ex) {
    var_dump('ERROR: '.$ex->getMessage());
}
//string(160) "ERROR: Symbolic link '/naughty' could not be created. Returned:
//  Failed to run command. Returned: ln: failed to create symbolic link 'naughty': Permission denied"