Home >Backend Development >PHP Tutorial >php 处置文件上传

php 处置文件上传

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:52:00712browse

php 处理文件上传
/**
* @todo : 处理文件上传
* @Author : yuguibin
* @date : 2014-03-20
*/
if(!empty($_FILES["file"]))
{
    $files = $_FILES["file"];//文件名称,file框的名称
    if(!empty($files['error'])){//错误信息
         switch ($files['error'])
            {
                case "1": $error = array('status'=>FALSE,'msg'=> "传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值");  break;
                case "2": $error = array('status'=>FALSE,'msg'=> "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值");  break;
                case "3": $error = array('status'=>FALSE,'msg'=> "文件只有部分被上传");  break;
                case "4": $error = array('status'=>FALSE,'msg'=> "没有文件被上传");   break;
                case "6": $error = array('status'=>FALSE,'msg'=> "找不到临时文件夹");  break;
                case "7": $error = array('status'=>FALSE,'msg'=> "文件写入失败");  break;
                default:  $error = array('status'=>FALSE,'msg'=> "未知错误");  break;
            }
             return $error;
    }
    $path = "upload/";//上传路径
    if(!file_exists($path))
    {
        if(! mkdir($path,0700))
        {
            $info = array("status"=>FALSE,"msg"=>"存放文件目录不存在");
            return $info;
        }
    }
    //允许上传的文件格式
    $extension = strtolower(end(explode("/",$files["type"])));
    $tp = array("gif","jpeg","jpg","png","bmp");
    if(!in_array($extension, $tp))
    {
        $info = array("status"=>FALSE,"msg"=>"文件格式错误");
        return $info;
    }
    $filename = $path.date("YmdHis").".".$extension;
    if(@move_uploaded_file($files["tmp_name"], $filename))
    {
        $info = array("status"=>TRUE,"msg"=>$filename);
        return $info;
    }
    else
    {
        $info = array("status"=>FALSE,"msg"=>"移动文件失败");
        return $info;
    }
}
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn