开发者社区> 问答> 正文

php下载文件如何设置文件名

比如oss上有个文件a.zip  下载保存的默认的文件名也是a.zip  如果让用户点击后下载保存文件名变为b.zip

展开
收起
崔吉雄 2016-07-08 13:56:47 4976 0
1 条回答
写回答
取消 提交回答
  • PHP SDK文档:
    https://help.aliyun.com/document_detail/32104.html?spm=5176.product8314910_31815.6.378.Yn03UD

    PHP下载的代码
    function getObjectToLocalFile($ossClient, $bucket)
    {
        $object = "a.zip";
        $localfile = "a.zip;
        $options = array(
            OssClient::OSS_FILE_DOWNLOAD => $localfile,
        );
        try{
            $ossClient->getObject($bucket, $object, $options);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");
    }

    用户点击另存为需要在Put的时候设置Content-Disposition", "attachment;filename=b.zip":

    设置在options中
    /**
         * 上传内存中的内容
         *
         * @param string $bucket bucket名称
         * @param string $object objcet名称
         * @param string $content 上传的内容
         * @param array $options
         * @return null
         */
        public function putObject($bucket, $object, $content, $options = NULL)
        {
            $this->precheckCommon($bucket, $object, $options);
            OssUtil::validateContent($content);
            $options[self::OSS_CONTENT] = $content;
            $options[self::OSS_BUCKET] = $bucket;
            $options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
            $options[self::OSS_OBJECT] = $object;
            if (!isset($options[self::OSS_LENGTH])) {
                $options[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
            } else {
                $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
            }
            if (!isset($options[self::OSS_CONTENT_TYPE])) {
                $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
            }
            $response = $this->auth($options);
            $result = new PutSetDeleteResult($response);
            return $result->getData();
        }
    2016-07-08 17:02:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-深入浅出网络编程与swoole内核-吴镇宇 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载