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

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

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、重慶小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了都安免費建站歡迎大家使用!

上傳文件到指定的目錄命名,比如我需要上傳一個文件到/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

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

名稱欄目:如何用CMS上傳文件到指定位置并固定命名
分享地址:http://www.muchs.cn/article36/ghodpg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設小程序開發(fā)、面包屑導航、網(wǎng)站營銷、服務器托管、云服務器

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)