我有一个关于fopen()和base64通信的问题。场景是这样的:我有一个必须从url获取资源(png/jpeg或pdf)的服务A。代码是这样的:
$uri = urldecode($_POST['uri']);
$imgfile = $uri;
$handle = fopen($uri, 'r');
$imagebinary = '';
while (!feof($handle)) {
$c = fgetc($handle);
if($c === false) break;
$imagebinary .= $c;
}
fclose($handle);
$return = base64_encode($imagebinary);现在,我有了JQUERY函数,它将这个服务(类似于:'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs') )发送到另一个名为B。B $return的服务。在服务B尝试在亚马逊s3上保存文件的特定情况下,代码是:
// in $imagedata is saved the string generated by service A
$imagedata = $_POST['serviceA_base64encodedfile'];
// $contentType taken from switch function on $ext
// for example 'image/png'
$filename = sha1(uniqid()) . '.' . $ext;
$full_filename = $path . '/' . $filename;
$stream = fopen('data://' . $contentType . ';base64,' . $imagedata, 'r');
fseek($stream, 0);
$opt = array(
'fileUpload' => $stream,
'acl' => AmazonS3::ACL_PUBLIC,
'contentType' => $contentType
);
$s3 = new AmazonS3(AWS_KEY, AWS_SECRET_KEY);
$response = $s3->create_object($bucket, $filename, $opt);但是被保存的图像是损坏的,另外,这个图像或pdf的字节数比原始的少。
我真的需要帮助:
发布于 2012-01-12 00:31:07
我不能百分之百确定这是否可行,但为什么不将数据base64_decode回二进制,然后将数据写入一个临时文件,并从该位置将其发送到亚马逊。类似于(未测试的):
// in $imagedata is saved the string generated by service A
$imagedata = base64_decode($_POST['serviceA_base64encodedfile']);
if (!$imagedata){
//Handle invalid base64 encoded data
}
// $contentType taken from switch function on $ext
// for example 'image/png'
$filename = sha1(uniqid()) . '.' . $ext;
$full_filename = $path . '/' . $filename;
$tmpfname = tempnam("/tmp", "image_to_upload");
$populated = file_put_contents($tmpfname,$imagedata);
if (!$populated){
//handle write failures
}
$opt = array(
'fileUpload' => "/tmp/".$tmpfname,
'acl' => AmazonS3::ACL_PUBLIC,
'contentType' => $contentType
);
$s3 = new AmazonS3(AWS_KEY, AWS_SECRET_KEY);
$response = $s3->create_object($bucket, $full_filename, $opt);我还假设在最后一次调用时,您希望在s3服务器上存储文件的位置是$full_filename……尽管您可以只使用$file_name。
https://stackoverflow.com/questions/8821089
复制相似问题