Search code examples
phparraysforeachreturnuploader

Return filenames from class.uploader.php


I've been trying everything, and it's starting to get on my nerves. I'm still trying to learn the ropes within PHP, so please bear with me.

I'm using the class.uploader.php to upload files to my server. It works as it should, but I want to just return the uploaded file(s) when the upload is complete. This is where I'm stuck.

This is my code;

<?php
include('assets/plugins/filer/php/class.uploader.php');

$uploader = new Uploader();
$data = $uploader->upload($_FILES['files'], array(
    'limit' => 10, //Maximum Limit of files. {null, Number}
    'maxSize' => 10, //Maximum Size of files {null, Number(in MB's)}
    'extensions' => null, //Whitelist for file extension. {null, Array(ex: array('jpg', 'png'))}
    'required' => false, //Minimum one file is required for upload {Boolean}
    'uploadDir' => 'upload/', //Upload directory {String}
    'title' => array('name'), //New file name {null, String, Array} *please read documentation in README.md
    'removeFiles' => true, //Enable file exclusion {Boolean(extra for jQuery.filer), String($_POST field name containing json data with file names)}
    'replace' => false, //Replace the file if it already exists  {Boolean}
));

if($data['isComplete']){

    $info = $data['data'];
    echo '<pre>';
    print_r($info);
    echo '</pre>';
}

if($data['hasErrors']){
    $errors = $data['errors'];
    print_r($errors);
}
?>

When upload completes, this is what it returns;

Array
(
[files] => Array
    (
        [0] => upload/  - TEST - #01.wav
        [1] => upload/  - TEST - #02.wav
    )

[metas] => Array
    (
        [0] => Array
            (
                [date] => Wed, 22 Feb 2017 14:03:42 +0100
                [extension] => wav
                [file] => upload/  - TEST - #01.wav
                [name] =>   - TEST - #01.wav
                [old_name] =>   - TEST - #01.wav
                [replaced] => 
                [size] => 1244204
                [size2] => 1.19 MB
                [type] => Array
                    (
                        [0] => audio
                        [1] => wav
                    )

            )

        [1] => Array
            (
                [date] => Wed, 22 Feb 2017 14:03:42 +0100
                [extension] => wav
                [file] => upload/  - TEST - #02.wav
                [name] =>   - TEST - #02.wav
                [old_name] =>   - TEST - #02.wav
                [replaced] => 
                [size] => 1677356
                [size2] => 1.60 MB
                [type] => Array
                    (
                        [0] => audio
                        [1] => wav
                    )

            )

    )

)

What I want to return is something like this;

Upload complete.
The following files has been uploaded:
- TEST - #02.wav
- TEST - #02.wav

Been trying for a while now, but I can't figure it out. Anyone care to help out? I've tried "echo $_FILES['files']", but that didn't do the trick.


Solution

  • You have to loop through the $data['data']['metas'] with the for() function. Like example given below:

    ...
    Upload complete.
    The following files has been uploaded:
    
    <?php
    for($i = 0; $i < count($data['data']['metas']); $i++)
    {
        echo "- ".$data['data']['metas'][$i]["name"]."<br />";
    }
    ?>
    

    Hope this will help!