迅睿CMS 上傳文件:上傳到指定位置-隨機命名

 

上傳文件到指定的目錄命名,比如我需要上傳一個文件到/cache/年月日/隨機名稱.

 

1、創建控制器:/dayrui/App/Demo/Controllers/Upload.php

<?php namespace Phpcmf\Controllers\Admin;

class Upload extends \Phpcmf\Common
{

    // 上傳界面
    public function index() {

        \Phpcmf\Service::V()->assign('upload_url', dr_url('demo/upload/add'));
        \Phpcmf\Service::V()->display('upload.html');
    }

    // 上傳處理
    function add() {

        $rt = \Phpcmf\Service::L('upload')->upload_file([
            'save_path' => WRITEPATH, // 上傳的固定文件路徑
            'form_name' => 'file_data', // 固定格式
            'file_exts' => ['txt'], // 上傳的擴展名
            'file_size' => 10 * 1024 * 1024, // 上傳的大小限制
            'attachment' => \Phpcmf\Service::M('Attachment')->get_attach_info('null'), // 固定文件時必須這樣寫
        ]);
        if (!$rt['code']) {
            // 失敗了
            exit(dr_array2string($rt));
        }

        // 上傳成功了
        exit(dr_array2string($rt));
    }


}

 

2、創建模板文件:/dayrui/App/Demo/Views/upload.html

{template "header.html"}

<link href="{ROOT_THEME_PATH}assets/global/plugins/jquery-fileupload/css/jquery.fileupload.css" rel="stylesheet" type="text/css" />
<script src="{ROOT_THEME_PATH}assets/global/plugins/jquery-fileupload/js/jquery.fileupload.js" type="text/javascript"></script>
<div class="dev" id="fileupload">
    <a href="___JavaScript:;" class="fileinput-button btn read"> <i class="fa fa-upload"></i> {dr_lang('上傳文件')}<input type="file" name="file_data"> </a>
</div>
<script type="text/javascript">
    $(function() {
        $("#fileupload").fileupload({
            disableImageResize: false,
            autoUpload: true,
            maxFileSize: "10000000000",
            url: "{$upload_url}",
            dataType: "json",
            acceptFileTypes: "*",
            maxChunkSize: 0,
            progressall: function (e, data) {
                // 上傳進度條 all
            },
            add: function (e, data) {
                $(".fileupload-progress").hide();
                data.submit();
            },
            done: function (e, data) {
                if (data.result.code > 0) {
                    dr_tips(data.result.code, data.result.msg);
                } else {
                    dr_tips(data.result.code, data.result.msg, -1);
                }
            },
            fail: function (e, data) {
                //console.log(data.errorThrown);
                dr_tips(0, "系統故障:"+data.errorThrown, -1);
                layer.closeAll('tips');

            },
        });
    });
</script>


{template "footer.html"}

 

3、訪問上傳界面:

/admin.php?s=demo&c=upload&m=index

 

 

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章