cnicollonline

"I was just guessing at numbers and figures, pulling the puzzles apart"

I was tasked with a project at work in which I had to build a site that would be quite dynamic. The problem however was that I could not use a database to hold information. What I did (and something I would avoid if I could) was create a class that looks within a folder thats specified and builds an array off the files within the folder.

How it works

Reads a folder of files, breaks apart the file name by a specified “breaker”. It then creates a new array off the specified file name attributes

Demo

File setup:

include_once('FileDetail.class.php');
// a new object reference using an array for the parameter which specifies the file name attributes
// FileDetail(array(attributes));
$files = new FileDetail(array('id','movie','name','ext'));

// loadFiles(Folder, separator)
$files->loadFiles('test_files2','_');

// New variable to hold the file name array
$fileNames = $files->getFileNames();

// New Variable to hold the file details array
$fileDetails = $files->getFileDetails();

Testing the arrays:

// ========================================= EXAMPLE =========================================
echo '<h4>File Names Test 2</h4>';
echo '<pre>';
	print_r($fileNames);
echo '</pre>';

// ========================================= EXAMPLE =========================================
echo '<h4>File Details</h4>';
echo '<pre>';
	print_r($fileDetails);
echo '</pre>';

File Names

Array
(
    [0] => 01_wall-e_walle02.jpg
    [1] => 02_batman_joker02.jpg
    [2] => 02_wall-e_walle.jpg
    [3] => 04_batman_joker02.jpg
    [4] => 05_batman_joker.jpg
)

File Details

Array
(
    [0] => Array
        (
            [id] => 01
            [movie] => wall-e
            [name] => walle02
            [ext] => jpg
        )

    [1] => Array
        (
            [id] => 02
            [movie] => batman
            [name] => joker02
            [ext] => jpg
        )

    [2] => Array
        (
            [id] => 02
            [movie] => wall-e
            [name] => walle
            [ext] => jpg
        )

    [3] => Array
        (
            [id] => 04
            [movie] => batman
            [name] => joker02
            [ext] => jpg
        )

    [4] => Array
        (
            [id] => 05
            [movie] => batman
            [name] => joker
            [ext] => jpg
        )

)

Real World Example:

echo '<h4>Get files of a specific parameter</h4>';
echo '<h5> movie -> batman</h5>';
echo '<pre>';
	print_r($files->getFilesByKey('movie','batman'));
echo '</pre>';

Get files of a specific parameter (movies->batman)

Array
(
    [0] => Array
        (
            [id] => 02
            [movie] => batman
            [name] => joker02
            [ext] => jpg
        )

    [1] => Array
        (
            [id] => 04
            [movie] => batman
            [name] => joker02
            [ext] => jpg
        )

    [2] => Array
        (
            [id] => 05
            [movie] => batman
            [name] => joker
            [ext] => jpg
        )

)

Conclusion

The class can be used in many which ways whether it’s loading a gallery of images or building a website based off of the attributes of the images. The class and demo files can be downloaded here

Write a Comment