python - 云平台不支持写操作,动态生成的文件怎么存储?
PHP中文网
PHP中文网 2017-04-17 13:04:39
0
3
437

如题,比如根据用户信息生成二维码,生成静态文件,再调用。可是云平台不支持写操作,所以图片无法本地保存,也就无法保存,大家有没有解决办法?如果不要求持久化,干脆不生成图片,base64 会报类型错误,TypeError: must be convertible to a buffer, not PilImage,这个要怎么做?
还有如果用云存储,也要生成图片才能上传上去。

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
大家讲道理

I feel like this is not a general idea. Most cloud platforms have storage and you just need to use this function

左手右手慢动作

If it is Sina Cloud, refer to the thumbnail generation and processing imagegif/imagejpeg/imagepng on SAE, which is similar.

Peter_Zhu

You can generate a temporary file (the system temporary directory, it is impossible to block this directory, otherwise the file cannot be uploaded) and write the binary content of the image into it. Then submit this temporary file and delete it after completion. For example (PHP example):

class file {

    private $file = null;

    public function __construct($content=null) {
        $this->file = tempnam(sys_get_temp_dir(),md5(uniqid().mt_rand()));
        if ($content) {
            $this->write($content);
        }
    }

    public function write($content) {
        file_put_contents($this->file,$content);
    }

    public function read($content) {
        return file_get_contents($this->file);
    }

    public function getFilepath() {
        return $this->file;
    }

    public function __destruct() {
        @unlink($this->file);
    }
}
$file = new file();
$file->write(图片二进制);

// cURL上传图片
// CURL_POSTFIELDS : @$file->getFilePath()

python. Don’t know

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template