开发者社区> 问答> 正文

分享一个超简单的php上传图片代码

只是需要一个简单的上传图片功能,
官方的那个php SDK有点多啊,
自己随便写了一个,用的put_object接口
不说了,上代码
<?php

class OSS
{
    const AK = 'xxx';
    const SK = 'xxxxxxxxxxxxx';
    private $_date = '';
    private $_ext = '';
    private $_mime = '';
    private $_file = '';
    private $_savaName = null;
    private $_bucket = null;
    private $url = '';
    private $_data = '';
    public function __construct($url, $bucket, $path, $name = null)
    {
        $this->url = $url;
        $this->_bucket = $bucket;
        $this->_date = gmdate('D, d M Y H:i:s T');
        $this->_file = $path;
        $this->_savaName = $name;
        $this->getExt();

        if (is_null($name)) {
            $this->_savaName = uniqid();
        } else {
            $this->_savaName = $name;
        }
        $this->_data = file_get_contents($path);


    }

    private function getExt()
    {
        $imageTypeArray = array
        (
            0 => 'UNKNOWN',
            1 => 'GIF',
            2 => 'JPEG',
            3 => 'PNG',
            4 => 'SWF',
            5 => 'PSD',
            6 => 'BMP',
            7 => 'TIFF_II',
            8 => 'TIFF_MM',
            9 => 'JPC',
            10 => 'JP2',
            11 => 'JPX',
            12 => 'JB2',
            13 => 'SWC',
            14 => 'IFF',
            15 => 'WBMP',
            16 => 'XBM',
            17 => 'ICO',
            18 => 'COUNT'
        );

        $info = getimagesize($this->_file);
        if(!isset($info[2])) {
            throw new Exception('getimagesize error');
        }
        $mineNO =$info[2];
        if (!isset($imageTypeArray[$mineNO]) || $imageTypeArray[$mineNO] === 0) {
            throw new Exception('unknown type');
        }
        $this->_mime = $info['mime'];
        $this->_ext = strtolower($imageTypeArray[$mineNO]);

    }

    private function hmacSignature($encodedPutPolicy)
    {
        return base64_encode(hash_hmac("sha1", $encodedPutPolicy, self::SK, true));
    }

    private function getHeaders()
    {
        $send = array();
        $send[] = sprintf('PUT /%s.%s HTTP/1.1', $this->_savaName, $this->_ext);
        $send[] = sprintf('HOST: %s.%s', $this->_bucket, $this->url);
        $send[] = "Cache-control: no-cache";
        $send[] = sprintf('Date: %s', $this->_date);
        $send[] = "Content-Encoding: utf-8";
        $send[] = sprintf('Content-Type: %s', $this->_mime);
        $send[] = sprintf('Content-Length: %s', strlen($this->_data));
        $send[] = "Connection: Close";
        return $send;
    }

    public function putObject()
    {
        $_arr = array(
            'PUT',
            '',
            $this->_mime,
            $this->_date,
            sprintf('/%s/%s.%s', $this->_bucket, $this->_savaName, $this->_ext)

        );

        $signature = $this->hmacSignature(implode("\n", $_arr));
        $authorization = "OSS " . self::AK . ":" . $signature;
        $headers = $this->getHeaders();
        $headers[] = 'Authorization: ' . $authorization . "\r\n\r\n";
        $data = implode("\r\n", $headers) . $this->_data;
//        echo sprintf('%s.%s', $this->_bucket, $this->url);
//        echo $data;
        $fp = fsockopen(sprintf('%s.%s', $this->_bucket, $this->url), 80, $errno, $errstr, 3);
        if (!$fp) {
           // echo "$errstr ($errno) \n";
            throw new \Exception($errstr,$errno);
        }
        fwrite($fp, $data);
        $buffer = array();
        while (!feof($fp)) {
            $buffer[] = fgets($fp, 128);
        }
        fclose($fp);
        if (count($buffer) > 0 && $buffer[0] === "HTTP/1.1 200 OK\r\n") {
            return true;
        } else {
            return false;
        }
    }

}


$oss = new OSS('oss-cn-beijing.aliyuncs.com', '你上传的bucket', '本地地址','保存名(不包含后缀)');
$result = $oss->putObject();
var_dump($result);


展开
收起
ap2483s1b 2018-04-04 13:07:35 2132 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载