溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何用CMS上傳文件到指定位置并固定命名

發(fā)布時間:2021-10-13 15:48:56 來源:億速云 閱讀:146 作者:iii 欄目:編程語言

這篇文章主要介紹“如何用CMS上傳文件到指定位置并固定命名”,在日常操作中,相信很多人在如何用CMS上傳文件到指定位置并固定命名問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何用CMS上傳文件到指定位置并固定命名”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

上傳文件到指定的目錄命名,比如我需要上傳一個文件到/cache/my.txt,固定的位置,固定的命名。

1、創(chuàng)建控制器:/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() {

        $file = WRITEPATH.'my.txt';
        $rt = \Phpcmf\Service::L('upload')->upload_file([
            'save_file' => $file, // 上傳的固定文件路徑
            '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、創(chuàng)建模板文件:/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, "系統(tǒng)故障:"+data.errorThrown, -1);
                layer.closeAll('tips');

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


{template "footer.html"}

3、訪問上傳界面:

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

到此,關(guān)于“如何用CMS上傳文件到指定位置并固定命名”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

cms
AI