当前位置 : 主页 > 网络编程 > JavaScript >

异步上传文件

来源:互联网 收集:自由互联 发布时间:2021-06-28
页面 选择文件: 服务端 function upload($name='file',$size=102400000){ $file=dirname(__FILE__); switch ($_FILES[$name]['type']) { case 'application/msword': case 'application/vnd.openxmlformats-officedocument.wordprocessingml.documen
页面
 

    

   
        选择文件:
服务端
function upload($name='file',$size=102400000){
    $file=dirname(__FILE__);
    switch ($_FILES[$name]['type']) {
        case 'application/msword':
        case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
            $path='file/doc/';
            $type='文档';
            break;
            
            case 'image/png':
            case 'image/gif':
            case 'image/jpeg':
                $path='file/img/';
                $type='图片';
                break;
                
            case 'application/vnd.ms-excel':
            case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
                $path='file/excel/';
                $type='文档';
                break;
                    
        default:
            return ['info'=>"文件格式错误!",'state'=>0];
            break;
    }
    $s=explode('.',$_FILES[$name]['name']);
    $name1=md5(mt_rand(1000,9999).time().mt_rand(1000,9999));
    if(!is_dir($file."/../../".dirname($path.$name1))){
        if (mkdir($file."/../../".$path, 0755, true)) {
        } else {
            return ['info'=>"目录 {$file}{$path} 创建失败!",'state'=>0];
        }
    }

    if (($_FILES[$name]["size"] < $size)) {
        if ($_FILES[$name]["error"] > 0) {
            return ['info'=>"Return Code: " . $_FILES[$name]["error"],'state'=>0];
        }else {
            if (file_exists($file."/../../".$path. $name1)) {
                return ['info'=>$path. $name1 . " already exists. ",'state'=>0];
            }else {
                move_uploaded_file($_FILES[$name]["tmp_name"],$file."/../../".$path .$name1.".".$s[count($s)-1]); 
                return ['info'=>$path .$name1.".".$s[count($s)-1],'state'=>1,'type'=>$type,'ms'=>$file."/../../".$path .$name1.".".$s[count($s)-1]]; 
            }
        }
    }else {
        return ['info'=>"Invalid file",'state'=>0];
    }
}
网友评论