Sunday, November 3, 2013

codeigniter media file manager example , php , json

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class media extends HO_Admin {

    public function __construct() {
        parent::__construct();

        $this->load->config('uploader');
        $this->load->model(array('media_model', 'mediafolder_model'));
        $this->load->library('NetAdvDirectoryService', NULL, '_directoryService');
        $this->load->library('NetAdvImageService', NULL, '_imageService');

        $this->AllowedFileTypes = $this->config->item('AllowedFileTypes');
        $this->load->helper('file');
    }

    public function index() {
        $this->view();
    }

    public function browser($dir = 0) {
        if (empty($dir)) {
            $dir = 0;
        }
       
        $selected_dir = $this->mediafolder_model->get($dir);       
        if(!empty($selected_dir)) {
            $selected_dir['selected_dir_breadcrumbs'] = $this->mediafolder_model->collect_parent_folders($selected_dir, NULL, TRUE);
        }
        $child_folders = $this->mediafolder_model->get_list(array('parent_id' => $dir));
        $child_medias = $this->media_model->get_list(array('folder_id' => $dir));

        $data = array();
        $data['selected_dir'] = $selected_dir;
        $data['dir_list'] = $child_folders;
        $data['file_list'] = $child_medias;

//        $files = scandir($dir);
//        foreach ($files as $file) {
//            if ($file != '.' && $file != '..') {
//                if (is_file($dir . $file)) {
//                    $info = pathinfo($dir . $file);
//                    $file_ext = strtolower($info['extension']);
//                    if (in_array($file_ext, $this->AllowedFileTypes)) {
//                        $data['file_list'][] = array(
//                            'basename' => $info['basename'],
//                            'extension' => $file_ext,
//                            'filename' => $info['filename']);
//                    }
//                } elseif (is_dir($dir . $file)) {
//                    $data['dir_list'][] = $file;
//                }
//            }
//        }
        $this->view('media/partial/filemanager-browser', $data);
    }

    public function delete() {
        $command = $this->input->get('file');
        if (!empty($command)) {
            try {
                if (is_dir($command)) {
                    if (is_dir_empty($command)) {
                        $this->success($command . ' санг устгачихлаа!');
                    } else {
                        $this->error($command . ' санд файл байгаа тул устгах боломжгүй');
                    }
                } elseif (is_file($command)) {
//                $this->load->model('article_media_model');
//                if ($this->article_media_model->media_in_article($command)) {
//                    $this->error($command . ' файл ашиглагдаж байгаа тул устгах боломжгүй');
//                }
                    unlink($command);
                    $this->success($command . ' файлыг устгачихлаа!');
                }
            } catch (Exception $ex) {
                $this->error($ex->getMessage());
            }
        }
    }

    public function rename() {
        $command = $this->input->get('filename');
        if (!empty($command)) {
            try {
                if (is_dir($command)) {
                    if (is_dir_empty($command)) {
                        $this->success($command . ' санг устгачихлаа!');
                    } else {
                        $this->error($command . ' санд файл байгаа тул устгах боломжгүй');
                    }
                } elseif (is_file($command)) {
//                $this->load->model('article_media_model');
//                if ($this->article_media_model->media_in_article($command)) {
//                    $this->error($command . ' файл ашиглагдаж байгаа тул устгах боломжгүй');
//                }
                    unlink($command);
                    $this->success($command . ' файлыг устгачихлаа!');
                }
            } catch (Exception $ex) {
                $this->error($ex->getMessage());
            }
        }
    }

    public function fileNameDialog() {
        $data = array();
        $data['command'] = $this->input->get('command');
        $data['filename'] = $this->input->get('filename');
        $this->view('media/partial/filename-dialog', $data);
    }

    public function qqupload($path = '') {
        if (empty($path)) {
            $this->view();
        } else {
            $path = trim($path) . '/';
            try {
                $inputHandler = false;

                $qqfile = $this->input->post('qqfile');
                if (empty($qqfile)) {
                    foreach ($_FILES as $file) {
                        $qqfile = $file["name"];
                        $ftype = $file["type"];
                        $fsize = $file["size"] / 1024;
                        $ftmp = $file["tmp_name"];

                        $inputHandler = fopen($ftmp, "rb");
                    }
                } else {
                    $inputHandler = fopen('php://input', "r");
                }

                // create a temp file where to save data from the input stream
                $fileHandler = fopen($path . $qqfile, "w+");

                // save data from the input stream
                while (true) {
                    $buffer = fgets($inputHandler, 4096);
                    if (strlen($buffer) == 0) {
                        fclose($inputHandler);
                        fclose($fileHandler);
                        return true;
                    }

                    fwrite($fileHandler, $buffer);
                }
                // done
            } catch (Exception $ex) {
                echo json_encode(array('success' => false, 'message' => $ex->getMessage()));
            }

            echo json_encode(array('success' => true));
        }
    }

    public function upload() {
        $this->load->library('UploadHandler');
    }

}

No comments: