dsbowmvth16379079 2012-12-03 21:05
浏览 22
已采纳

在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

  • 写回答

1条回答 默认 最新

  • duandong1963 2013-06-20 10:39
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)