在Redactor中使用带有fwrite的imageGetJson

Basicly what i am trying to accomplish is to get a list of uploaded images from a folder using the editor redactor with the function: imageGetJson

I have managed to get fwrite to add an array in my images.json file. Now the problem is that redactor needs brackets around the input in order to render the images.

So my question is how do i get the brackets around my data?

The upload file i have so far is this:

// This is a simplified example, which doesn't cover security of uploaded images. 
// This example just demonstrate the logic behind the process. 


// files storage folder
$dir = '../img/uploads/';

$_FILES['file']['type'] = strtolower($_FILES['file']['type']);

if ($_FILES['file']['type'] == 'image/png' 
|| $_FILES['file']['type'] == 'image/jpg' 
|| $_FILES['file']['type'] == 'image/gif' 
|| $_FILES['file']['type'] == 'image/jpeg'
|| $_FILES['file']['type'] == 'image/pjpeg')
{   
    // setting file's mysterious name
    $filename = md5(date('YmdHis')).'.jpg';
    $file = $dir.$filename;

    // copying
    copy($_FILES['file']['tmp_name'], $file);

    // displaying file    
    $array = array(
        'filelink' => '/img/uploads/'.$filename,
        'thumb' => '/img/uploads/'.$filename,
        'image' => '/img/uploads/'.$filename,
    );



    echo stripslashes(json_encode($array));

    $json = stripslashes(json_encode($array)), "
";
    $files = fopen('../img/uploads/images.json','a+');
    fwrite($files, $json . "
");
    fclose($files);
}

I have made an array that contains the file location and wrote it to the file images.json

The way i need it to look is like this

[
    {"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
    {"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
    {"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
    {"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
]

Only it adds like this:

[]
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}
{"filelink":"image.jpg","thumb":"image.jpg","image":"image.jpg"}

Hope someone can help me with this because i cant figure it out.

Thanks in advance :D

I found another solution: just change the data.json in a php-file data.php which reads the directories you want to list and then convert to json and echo.

Here is what I use now (demo-version, cause my paypal isn't running yet) (path should be set according to your settings)

In the page where you load the redactor:

imageGetJson: 'redactor901trial/demo/json/data.php'

$path = 'redactor901trial/demo/json/';
$upload_dir = 'images/';

$handle = opendir($upload_dir);

while ($file = readdir($handle)) {
   if(!is_dir($upload_dir.$file) && !is_link($upload_dir.$file)) {


    $docs[] = $file;
   }
}

sort($docs);

foreach($docs as $key=>$file){
    $array[] = array(
            'filelink' => $path.$upload_dir.$file,
            'thumb' => $path.$upload_dir.$file,
            'image' => $path.$upload_dir.$file,
            'folder' => 'Folder 5'
        ); 
        }   
echo stripslashes(json_encode($array));

Hope this helps and it is not too late ;)

Regards

Steven