Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将base64映像上载到amazon s3_Php_Image_Amazon S3_Upload_Base64 - Fatal编程技术网

Php 将base64映像上载到amazon s3

Php 将base64映像上载到amazon s3,php,image,amazon-s3,upload,base64,Php,Image,Amazon S3,Upload,Base64,我在尝试将图像上载到AWS S3时遇到一些问题。它似乎正确上传了文件,但每当我尝试下载或预览时,它都无法打开。目前,这是我正在使用的上传代码: <?php require_once 'classes/amazon.php'; require_once 'includes/aws/aws-autoloader.php'; use Aws\S3\S3Client; $putdata = file_get_contents("php://input"); $request = json_dec

我在尝试将图像上载到AWS S3时遇到一些问题。它似乎正确上传了文件,但每当我尝试下载或预览时,它都无法打开。目前,这是我正在使用的上传代码:

<?php
require_once 'classes/amazon.php';
require_once 'includes/aws/aws-autoloader.php';
use Aws\S3\S3Client;

$putdata = file_get_contents("php://input");
$request = json_decode($putdata);

$image_parts = explode(";base64,", $request->image);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = $image_parts[1];

$dateTime = new DateTime();
$fileName = $dateTime->getTimestamp() . "." . $image_type;  

$s3Client = S3Client::factory(array(
    'region' => 'eu-west-1',
    'version' => '2006-03-01',
    'credentials' => array(
        'key'    => Amazon::getAccessKey(),
        'secret' => Amazon::getSecretKey(),
    )
));

try {
    $result = $s3Client->putObject(array(
        'Bucket'          => Amazon::getBucket(),
        'Key'             => 'banners/' . $fileName,
        'Body'            => $image_base64,
        'ContentType'     => 'image/' . $image_type,
        'ACL'             => 'public-read'
    ));
    echo $result['ObjectURL'] . "\n";
} catch(S3Exception $e) {
    echo $e->getMessage() . "\n";
}
?>

因此,当我上传图像文件后检查控制台时,它具有预期的大小、权限和标题,但正如我所说的,每当我尝试打开文件时,它都会失败


这里有什么问题?提前感谢。

这里的问题是您似乎正在上载图像的base64编码版本,而不是图像的原始字节。首先将$image_base64解码为原始字节。我敢肯定,如果您试图在文本编辑器中打开这些“图像”,您将看到base64十六进制数据。

您可以使用函数$s3Client->upload动态上载这些图像,如以下示例所示:

<?php
$bucket = 'bucket-name';
$filename = 'image-path.extension';
$imageData = base64_decode(end(explode(",", $base64)));
$upload = $s3Client->upload($bucket, $filename, $imageData, 'public-read');
$upload->get('ObjectURL');

我使用silex php 2.0和以下代码源

    $s3 = $app['aws']->createS3();
    $putdata = file_get_contents("php://input");
    $data = json_decode($request->getContent(), true);
    $data = (object) $data;
    $image_parts = explode(";base64,", $data->image);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];
    $image_base64 = base64_decode($image_parts[1]);
    $result = $s3->putObject([
        'ACL' => 'public-read',
        'Body' => $image_base64,
        'Bucket' => 'name-bucket',
        'Key' => 'test_img.jpeg',
        'ContentType' => 'image/' . $image_type,
    ]);
    var_dump($result['ObjectURL']);  
嗨,Oldwill19,“fount”是什么?