关于在http://www.thinkphp.cn/info/194.html中看到的"上传文件的命名规范"不是很明白,是关于saveRule的用户自定义函数,所以自己尝试添加了一些代码,不对的地方,还请大家指出!!
这是我的上传图片的配置代码:
这是效果图:
1假如你的项目中使用上传文件的方法比较少的话,你可以不修改代码的,
直接设置上传文件名(参考来自:http://www.thinkphp.cn/info/194.html)$upload->saveRule = time().'_'.mt_rand();
2.修改Upload.class.php文件,适合多处上传需要用到用户自定义函数
修改的文件是:Upload.class.php(Thinkphp的文件上传文件类)
我在class中添加了一个方法:
类中的$config数组中添加了一个元素:'userFunction' => false //用户是否使用自定义函数
这是自己添加的方法:/**<br>
* 存放用户自定义函数定义<br>
*@param null<br>
*@return Array(functionName,param)<br>
*Add on 2014-03-15 By funInCorner(xiaofosong@126.com)<br>
* **/<br>
private function __isfExists(){<br>
//返回的数组(函数名称,函数参数)<br>
$re_arr = array("functionName"=>"","param"=>"");<br>
$param2 = "";<br>
//将用户的传递的saveRule赋值给$functionName<br>
$functionName = $saveRule = $this->saveRule;<br>
//$this->userFunction是用来判断用户是否用用户自定义函数<br>
if(strpos($saveRule, "(" ) != false && $this->userFunction){<br>
//获取开始位置<br>
$startLeft = strpos($saveRule, "(" );<br>
//参数字符串 <br>
$param2 = substr($functionName,$startLeft + 1,strlen($functionName) - ($startLeft + 2) ); <br>
//函数名称<br>
$functionName = substr($functionName,0,strpos($saveRule, "(" ));<br>
$re_arr['functionName'] = $functionName;<br>
$re_arr['param'] = $param2; <br>
}else{<br>
$re_arr['functionName'] = $this->saveRule;<br>
}<br>
<br>
/**<br>
*下面的方法需要手动加的<br>
*用户自定义函数的返回值务必是string<br>
*返回的string,将作为不带扩展名的文件名称(如:helloWorld)<br>
*范例配置:$upload->saveRule = 'datetime(10)';<br>
*或者是:$upload->saveRule = 'datetime()';<br>
**/<br>
if($param2){<br>
//当用户自定义的函数有参数时<br>
if(!function_exists($functionName)){<br>
//只是范例方法,可以删除<br>
function datetime($param2){<br>
return $param2.date('YmdHis').rand(0,10);<br>
};<br>
} <br>
}else{<br>
//当用户自定义的函数没有参数时<br>
if(!function_exists($functionName)){<br>
//只是范例方法,可以删除<br>
function datetime(){<br>
return date('YmdHis').rand(0, 10);<br>
};<br>
} <br>
}<br>
return $re_arr;<br>
}
修改了getSaveName方法 //修改之后的getSaveName函数<br>
private function getSaveName($filename) {<br>
$re_arr = $this->__isfexists();//调用一下检查函数<br>
$rule = $re_arr['functionName'];<br>
$parma = $re_arr['param']; <br>
if(empty($rule)) {//没有定义命名规则,则保持文件名不变<br>
$saveName = $filename['name'];<br>
}else {<br>
if($parma != ""){<br>
if(function_exists($rule)) {<br>
//根据用户自定义的函数的string做文件名称<br>
$saveName = $rule($parma);<br>
}<br>
}else{<br>
if(function_exists($rule) ){<br>
//使用函数生成一个唯一文件标识号<br>
$saveName = $rule();<br>
}else {<br>
//使用给定的文件名作为标识号<br>
$saveName = $rule;<br>
}<br>
}<br>
//最后保存名称和文件扩展名用"."连接<br>
$saveName .= ".".$filename['extension'];<br>
}<br>
if($this->autoSub) {<br>
// 使用子目录保存文件<br>
$filename['savename'] = $saveName;<br>
$saveName = $this->getSubName($filename).$saveName;<br>
}<br>
return $saveName;<br>
}
UploadFile.class.zip
( 5.22 KB 下载:34 次 )
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
