最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php ajax 文件上传实现代码
时间:2010-11-29 编辑:简简单单 来源:一聚教程网
一款完整ajax 文件上传功能,我们利用了jquery ajax上传插件来实现的,下面代码非常完整。
do_ajax_file_upload.php
$fileelementname = 'filetoupload';
if(isset($_files['filetoupload'])){
if($_files['filetoupload']['name'] <> ""){
require_once ('upload_class.php');
$savepath = "../upfiles/{$_session["suploaddir"]}";
makedirectory($savepath);
$fileformat = array('gif','jpg','jpge','png','swf');
$maxsize = 0;
$overwrite = 0;
$f = new clsupload( $savepath, $fileformat, $maxsize, $overwrite);
$f->setthumb(0);
if (!$f->run('filetoupload',1)){
$error.=$f->errmsg();
}
$uparrays=$f->getinfo();
foreach ($uparrays as $uparray) {
$msg.=$uparray[path].$uparray[savename].",";
}
if($msg!=''){$msg=substr($msg,0,-1);}
echo "{";
echo "error: '" . $error . "',n";
echo "msg: '" . $msg . "'n";
echo "}";
}
}
function makedirectory($directoryname) {
$directoryname = str_replace("","/",$directoryname);
$dirnames = explode('/', $directoryname);
$total = count($dirnames) ;
$temp = '';
for($i=0; $i<$total; $i++) {
$temp .= $dirnames[$i].'/';
if (!is_dir($temp)) {
$oldmask = umask(0);
if (!mkdir($temp, 0777)) exit("不能建立目录 $temp");
umask($oldmask);
}
}
return true;
}下面代码保存成upload_class.php
'
class clsupload{
var $savename;// 保存名
var $savepath;// 保存路径
var $fileformat = array('gif','jpg','doc','application/octet-stream');// 文件格式&mime限定
var $overwrite = 0;// 覆盖模式
var $maxsize = 0;// 文件最大字节
var $ext;// 文件扩展名
var $thumb = 0;// 是否生成缩略图
var $thumb;// 缩略图宽
var $thumb;// 缩略图高
var $thumbprefix = "s_";// 缩略图前缀
var $errno;// 错误代号
var $returnarray= array();// 所有文件的返回信息
var $returninfo= array();// 每个文件返回信息function clsupload($savepath, $fileformat='',$maxsize = 0, $overwrite = 0) {
$this->setsavepath($savepath);
$this->setfileformat($fileformat);
$this->setmaxsize($maxsize);
$this->setoverwrite($overwrite);
$this->setthumb($this->thumb, $this->thumbwidth,$this->thumbheight);
$this->errno = 0;
}// 上传
function run($fileinput,$changename = 1){
if(isset($_files[$fileinput])){
$filearr = $_files[$fileinput];
if(is_array($filearr['name'])){//上传同文件域名称多个文件
for($i = 0; $i < count($filearr['name']); $i++){
$ar['tmp_name'] = $filearr['tmp_name'][$i];
$ar['name'] = $filearr['name'][$i];
$ar['type'] = $filearr['type'][$i];
$ar['size'] = $filearr['size'][$i];
$ar['error'] = $filearr['error'][$i];
$this->getext($ar['name']);//取得扩展名,赋给$this->ext,下次循环会更新
$this->setsavename($changename == 1 ? '' : $ar['name']);//设置保存文件名
if($this->copyfile($ar)){
$this->returnarray[] = $this->returninfo;
}else{
$this->returninfo['error'] = $this->errmsg();
$this->returnarray[] = $this->returninfo;
}
}
return $this->errno ? false : true;
}else{//上传单个文件
$this->getext($filearr['name']);//取得扩展名
$this->setsavename($changename == 1 ? '' : $filearr['name']);//设置保存文件名
if($this->copyfile($filearr)){
$this->returnarray[] = $this->returninfo;
}else{
$this->returninfo['error'] = $this->errmsg();
$this->returnarray[] = $this->returninfo;
}
return $this->errno ? false : true;
}
return false;
}else{
$this->errno = 10;
return false;
}
}
// 单个文件上传
function copyfile($filearray){
$this->returninfo = array();
// 返回信息
$this->returninfo['name'] = $filearray['name'];
$this->returninfo['savename'] = $this->savename;
$this->returninfo['size'] = number_format( ($filearray['size'])/1024 , 0, '.', ' ');//以kb为单位
$this->returninfo['type'] = $filearray['type'];
$this->returninfo['path'] = $this->savepath;
// 检查文件格式
if (!$this->validateformat()){
$this->errno = 11;
return false;
}
// 检查目录是否可写
if(!@is_writable($this->savepath)){
$this->errno = 12;
return false;
}
// 如果不允许覆盖,检查文件是否已经存在
if($this->overwrite == 0 && @file_exists($this->savepath.$filearray['name'])){
$this->errno = 13;
return false;
}
// 如果有大小限制,检查文件是否超过限制
if ($this->maxsize != 0 ){
if ($filearray["size"] > $this->maxsize){
$this->errno = 14;
return false;
}
}
// 文件上传
if(!@copy($filearray["tmp_name"], $this->savepath.$this->savename)){
$this->errno = $filearray["error"];
return false;
}elseif( $this->thumb ){//创建缩略图
$createfunction = "imagecreatefrom".($this->ext == 'jpg' ? 'jpeg' : $this->ext);
$savefunction = "image".($this->ext == 'jpg' ? 'jpeg' : $this->ext);
if (strtolower($createfunction) == "imagecreatefromgif"
&& !function_exists("imagecreatefromgif")) {
$this->errno = 16;
return false;
} elseif (strtolower($createfunction) == "imagecreatefromjpeg"
&& !function_exists("imagecreatefromjpeg")) {
$this->errno = 17;
return false;
} elseif (!function_exists($createfunction)) {
$this->errno = 18;
return false;
}
$original = @$createfunction($this->savepath.$this->savename);
if (!$original) {$this->errno = 19; return false;}
$originalheight = imagesy($original);
$originalwidth = imagesx($original);
$this->returninfo['originalheight'] = $originalheight;
$this->returninfo['originalwidth'] = $originalwidth;
if (($originalheight < $this->thumbheight
&& $originalwidth < $this->thumbwidth)) {
// 如果比期望的缩略图小,那只copy
copy($this->savepath.$this->savename,
$this->savepath.$this->thumbprefix.$this->savename);
} else {
if( $originalwidth > $this->thumbwidth ){// 宽 > 设定宽度
$thumbwidth = $this->thumbwidth ;
$thumbheight = $this->thumbwidth * ( $originalheight / $originalwidth );
if($thumbheight > $this->thumbheight){//高 > 设定高度
$thumbwidth = $this->thumbheight * ( $thumbwidth / $thumbheight );
$thumbheight = $this->thumbheight ;
}
}elseif( $originalheight > $this->thumbheight ){//高 > 设定高度
$thumbheight = $this->thumbheight ;
$thumbwidth = $this->thumbheight * ( $originalwidth / $originalheight );
if($thumbwidth > $this->thumbwidth){//宽 > 设定宽度
$thumbheight = $this->thumbwidth * ( $thumbheight / $thumbwidth );
$thumbwidth = $this->thumbwidth ;
}
}
if ($thumbwidth == 0) $thumb;
if ($thumbheight == 0) $thumb;
$createdthumb = imagecreatetruecolor($thumbwidth, $thumbheight);
if ( !$createdthumb ) {$this->errno = 20; return false;}
if ( !imagecopyresampled($createdthumb, $original, 0, 0, 0, 0,
$thumbwidth, $thumbheight, $originalwidth, $originalheight) )
{$this->errno = 21; return false;}
if ( !$savefunction($createdthumb,
$this->savepath.$this->thumbprefix.$this->savename) )
{$this->errno = 22; return false;}
}
}
// 删除临时文件
if(!@$this->del($filearray["tmp_name"])){
return false;
}
return true;
}
// 文件格式检查,mime检测
function validateformat(){
if(!is_array($this->fileformat)
|| in_array(strtolower($this->ext), $this->fileformat)
|| in_array(strtolower($this->returninfo['type']), $this->fileformat) )
return true;
else
return false;
}
//获取文件扩展名
function getext($filename){
$ext = explode(".", $filename);
$ext = $ext[count($ext) - 1];
$this->ext = strtolower($ext);
}
//设置上传文件的最大字节限制 0:表示无限制
function setmaxsize($maxsize){
$this->maxsize = $maxsize;
}
//设置文件格式限定
function setfileformat($fileformat){
if(is_array($fileformat)){$this->fileformat = $fileformat ;}
}
//设置覆盖模式 1:允许覆盖 0:禁止覆盖
function setoverwrite($overwrite){
$this->overwrite = $overwrite;
}
//设置保存路径
function setsavepath($savepath){
$this->savepath = substr( str_replace("","/", $savepath) , -1) == "/" ? $savepath : $savepath."/";
}
//设置缩略图
function setthumb($thumb, $thumb,$thumb){
$this->thumb = $thumb;
if($thumbwidth) $this->thumbwidth = $thumbwidth;
if($thumbheight) $this->thumbheight = $thumbheight;
}
//设置文件保存名
function setsavename($savename){
if ($savename == ''){ // 如果未设置文件名,则生成一个随机文件名
$name = date('ymdhis')."_".rand(100,999).'.'.$this->ext;
} else {
$name = $savename;
}
$this->savename = $name;
}
//删除文件
function del($filename){
if(!@unlink($filename)){
$this->errno = 15;
return false;
}
return true;
}
// 返回上传文件的信息
function getinfo(){
return $this->returnarray;
}
// 得到错误信息
function errmsg(){
$uploadclasserror = array(
0 =>'there is no error, the file uploaded with success. ',
1 =>'the uploaded file exceeds the upload_max_filesize directive in php.ini.',
2 =>'the uploaded file exceeds the max_file_size that was specified in the html form.',
3 =>'the uploaded file was only partially uploaded. ',
4 =>'no file was uploaded. ',
6 =>'missing a temporary folder. introduced in php 4.3.10 and php 5.0.3. ',
7 =>'failed to write file to disk. introduced in php 5.1.0. ',
10 =>'input name is not unavailable!',
11 =>'the uploaded file is unallowable!',
12 =>'directory unwritable!',
13 =>'file exist already!',
14 =>'file is too big!',
15 =>'delete file unsuccessfully!',
16 =>'your version of php does not appear to have gif thumbnailing support.',
17 =>'your version of php does not appear to have jpeg thumbnailing support.',
18 =>'your version of php does not appear to have pictures thumbnailing support.',
19 =>'an error occurred while attempting to copy the source image .
your version of php ('.phpversion().') may not have this image type support.',
20 =>'an error occurred while attempting to create a new image.',
21 =>'an error occurred while copying the source image to the thumbnail image.',
22 =>'an error occurred while saving the thumbnail image to the filesystem.
are you sure that php has been configured with both read and write access on this folder?',
);
if ($this->errno == 0)
return false;
else
return $uploadclasserror[$this->errno];
}
}
-
上一个: jquery拖动保存数据实例代码
-
下一个: 简单ajax读取数据实现代码
相关文章
- php ajaxfileupload实现ajax文件上传功能 03-28
- php Ajax实现异步文件上传的代码 05-04
- 二款php ajax文件上传代码 09-26
- ajax php文件上传代码 09-26
- ajax+php文件上传代码 07-24
- HTML简单购物数量小程序代码展示 10-31