电话
13363039260
上传文件到指定的目录命名,比如我需要上传一个文件到/cache/my.txt,固定的位置,固定的命名。
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() {
$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、创建模板文件:/template/pc/default/home/demo/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、访问上传界面:
/index.php?s=demo&c=upload&m=index