thinkphp用于上传文件的代码。
<?php
/**
* 文件上传代码
* by http://www.
*/
class UploadFileAction extends Action{
public function index() {
$Photo = M('Photo');
$data = $Photo->order('create_time desc')->find();
$this->assign('data', $data);
$this->display();
}
public function upload() {
if (!empty($_FILES)) {
//如果有文件上传 上传附件
$this->_upload();
}
}
// 文件上传
protected function _upload() {
import('@.ORG.UploadFile');
//导入上传类
$upload = new UploadFile();
//设置上传文件大小
$upload->maxSize = 3292200;
//设置上传文件类型
$upload->allowExts = explode()(',', 'jpg,gif,png,jpeg');
//设置附件上传目录
$upload->savePath = './Uploads/';
//设置需要生成缩略图,仅对图像文件有效
$upload->thumb = true;
// 设置引用图片类库包路径
$upload->imageClassPath = '@.ORG.Image';
//设置需要生成缩略图的文件后缀
$upload->thumbPrefix = 'm_,s_'; //生产2张缩略图
//设置缩略图最大宽度
$upload->thumbMaxWidth = '400,100';
//设置缩略图最大高度
$upload->thumbMaxHeight = '400,100';
//设置上传文件规则
$upload->saveRule = 'uniqid';
//删除原图
$upload->thumbRemoveOrigin = true;
if (!$upload->upload()) {
//捕获上传异常
$this->error($upload->getErrorMsg());
} else {
//取得成功上传的文件信息
$uploadList = $upload->getUploadFileInfo();
import('@.ORG.Image');
//给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], APP_PATH.'Tpl/Public/Images/logo.png');
$_POST['image'] = $uploadList[0]['savename'];
}
$model = M('Photo');
//保存当前数据对象
$data['image'] = $_POST['image'];
$data['create_time']= NOW_TIME;
$list = $model->add($data);
if ($list !== false) {
$this->success('上传图片成功!');
} else {
$this->error('上传图片失败!');
}
}
}
?>
php递归遍历指定目录下的所有文件。
<?php
/**
* 递归查找目录下所有文件。
* site: http://www.
*/
function scanfiles($dirs,$path='',$root,&$files,&$options){
if($dirs==false){
return ;
}
foreach($dirs as $val){
if($val=='.' || $val=='..' || in_array($val,$options['exclude'])){
continue;
}
if(is_file($root.'/'.$path.$val)){
$files[]['name']=$path.$val;
}elseif(is_dir($root.'/'.$path.$val)){
if(empty($path)){
$nextpath=$val.'/';
}else{
$nextpath=$path.$val.'/';
}
$dir=$root.'/'.$nextpath;
scanfiles(@scandir($dir),$nextpath,$root,$files,$options);
}
}
}
?>
统计php代码的行数,过滤掉其中的注释。
<?php
/// 获取命令行参数
/// by http://www.
$fileName = $argv[1];
if ( ! is_dir( $fileName ) && ! file_exists( $fileName ) ) {
echo "Usage: ".$argv[0]." [path|file]\n";
exit;
}
$disableFile = "DMO|test_|FileZip|jpgraph|smarty|tourdata|demo_|Frame|socket.php|IDNA2.php|timezone.php|config.db.php|config.php";
$disableFile .= "|main1.php|obj.php|interface.php|hmac.php|php-excel.class.php|cfg.class.php|global.php|gconfig.php|main2.php|sinfo.php";
$disableFile .= "|main.php|index.php|mod_tpl.php|redis.lua|^_";
$disableDir = "cache$|templates$|templates_c$|adodb$|smarty$|mailer$|biz$|test$";
$ext = "\.php$|\.c$|\.h$|\.go$|\.lua$";
if ( is_dir( $fileName ) ) {
getDir( $fileName );
} else {
echoCode( $fileName );
}
function echoCode( $fileName ) {
$fileLines = file( $fileName );
$lineCount = 0;
foreach( $fileLines AS $line ) {
$line = str_replace()( "\t", " ", $line );
$tr = trim( $line );
if ( preg_match( "/\\*|^\*|^ {1,}\*|\/\*|\*\/|^ {1,}\/\/|^\/\//", $line ) || $tr === "" ) {
continue;
}
$lineCount++;
$line = str_replace( "\r", "", $line );
$line = str_replace( "\n", "", $line );
echo "".$line."\n";
}
echo "\n";
}
function getDir( $path ) {
global $disableFile, $disableDir, $ext;
$dDirs = dir( $path );
while ( false !== ( $fileDirs = $dDirs->read() ) ) {
$sCodeFile = $path . "/".$fileDirs;
if ( $fileDirs == "." || $fileDirs == ".." || preg_match("/".$disableDir."/", $fileDirs ) ) {
continue;
}
if ( ! is_dir( $sCodeFile ) && ! file_exists( $sCodeFile ) ) {
continue;
}
if ( preg_match( "/".$disableFile."/", $fileDirs ) ||
( ! is_dir( $sCodeFile ) && ! preg_match( "/".$ext."/", $fileDirs ) ) ) {
continue;
}
if ( is_dir( $sCodeFile ) ) {
getDir( $sCodeFile );
continue;
} else {
//echo $sCodeFile."\n";
echo iconv( "UTF-8", "GBK", "文件名: ").$fileDirs."\n";
//echo "文件名: ".$fileDirs."\n";
echoCode( $sCodeFile );
}
}
}
?>