php上传图片

32F  2018/12/19 php




    /*move_uploaded_file*/
    function one(){
        $path = date('Y/m/');
        $dir = ROOT . $path;
        $this->mkdirs($path);

        if($_FILES['image']['error']){
            echo json_encode(['status'=>0,'msg'=>'上传失败']);
        }else{
            $ext = str_replace('image/','',$_FILES['image']['type']);
            do {
                $filename = random(30) . '.' . $ext;
            } while (file_exists($dir . $filename));
            move_uploaded_file($_FILES["image"]["tmp_name"],$dir.$filename);
        }
    }

    /*fwrite*/
    function two(){
        $path = date('Y/m/');
        $dir = ROOT . $path;
        $this->mkdirs($path);

        $data = file_get_contents("php://input") ? file_get_contents("php://input") : '';

        if(!empty($data)){
            $ext = str_replace('image/','',$_GPC['type']);
            do {
                $filename = random(30) . '.' . $ext;
            } while (file_exists($dir . $filename));

            if(@$fp = fopen($dir.$filename,'w+')){
                fwrite($fp,$data);
                fclose($fp);
            }
        }
    }


    /*file_put_contents*/
    public function three($url,$ext){
        $path = date('Y/m/');
        $dir = ROOT . $path;
        $this->mkdirs($path);
        do {
            $filename = random(30) . '.' . $ext;
        } while (file_exists($dir . $filename));

        //存储图片
        $content = @file_get_contents($url);
        @file_put_contents($dir.$filename, $content);
     }

   /*新建文件夹*/
    function mkdirs($path) {
        $path = explode('/',$path);
        $np = ROOT;
        foreach ($path as $p){
            $np .= '/'.$p;
            if(!is_dir($np)){
                mkdir($np);
            }
        }
        return is_dir($np);
    }

添加评论
全部回复
暂无回复