龙空技术网

Ckeditor4上传文件函数

趣易编程 137

前言:

现时咱们对“apachemovefile函数”大体比较重视,朋友们都想要剖析一些“apachemovefile函数”的相关文章。那么小编也在网摘上搜集了一些对于“apachemovefile函数””的相关内容,希望我们能喜欢,你们一起来学习一下吧!

//图片上传函数

if( !function_exists( 'upload' ) ){

//有的版本的ckeditor要传callback参数

function upload( $uploadPath=null, $size=3 ){

if( !$uploadPath ) return;

if( !$_FILES ) return;

$uploadPath = ROOT . $uploadPath; // ROOT是服务器的本地根目录,需要先声明

$suffixs = array("jpg","bmp","gif","png"); // 可以上传的类型

$uploadFilename = $_FILES['upload']['name'];

$uploadFilesize = $_FILES['upload']['size'];

$suffix = pathInfo( $uploadFilename, PATHINFO_EXTENSION );

// size是限制上传文件的大小

if( $uploadFilesize <= $size*1024*1000 && in_array( $suffix, $suffixs ) )

moveFile( $uploadPath, $suffix );

else{

$msg = '只能上传格式为.jpg/.png/.bmp/.gif,';

$msg .= '并且小于'.$size.'M的文件';

responseFail( $msg );

}

}

}

//除图片外的文件上传函数

if( !function_exists( 'upfile' ) ){

function upfile( $uploadPath=null, $size=3 ){

if( !$uploadPath ) return;

$uploadPath = ROOT . $uploadPath; // ROOT是服务器的本地根目录,需要先声明

$uploadFilename = $_FILES['upload']['name'];

$uploadFilesize = $_FILES['upload']['size'];

if( $uploadFilesize > $size * 1024 * 1000 )

responseFail( "文件大小不能超过" . $size . 'M');

$suffix = pathInfo( $uploadFilename, PATHINFO_EXTENSION );

moveFile( $uploadPath, $suffix );

}

}

//上传成功,兼容新版和旧版CKeditor

if( !function_exists( 'responseSuc' ) ){

function responseSuc( $preView ){

$str = '';

//旧版的CK,也适合进行了config的form设置的新版

if( isset( $_GET['CKEditorFuncNum'] ) ){

$callback = $_GET['CKEditorFuncNum'];

$str .= "<script>";

$str .= "window.parent.CKEDITOR.tools.callFunction";

$str .= "($callback,'" . $preView . "','');</script>";

//新版的CK,不进行config设置时

}else{

$json['uploaded'] = 1;

$json['fileName'] = '';

$json['url'] = $preView;

$str = json_encode( $json );

}

echo $str;

}

}

//上传失败,兼容新老CK

if( !function_exists( 'responseFail' ) ){

function responseFail( $msg ){

//新版

if( !isset( $_GET['CKEditorFuncNum'] ) ){

$json['uploaded'] = '0';

$json['error'] = $msg;

echo json_encode( $json );

}else{

echo "<font color=\"red\"size=\"2\">*文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)</font>";

}

}

}

//移动文件

if( !function_exists( 'moveFile' ) ){

function moveFile( $upDir, $suffix ){

$uuid = str_replace( '.','',uniqid("",TRUE) ).".".$suffix;

$localFile = $upDir . $uuid;

move_uploaded_file( $_FILES['upload']['tmp_name'], $localFile );

//预览的图片地址不能是本地地址,所以要替换服务器地址

$preView = str_replace( ROOT, HOST, $localFile );// HOST是服务器的根目录,需要先设置

responseSuc( $preView );

}

}

标签: #apachemovefile函数