gpt4 book ai didi

java - PHP解码Base64,刷新并保留数据

转载 作者:行者123 更新时间:2023-11-30 09:47:51 25 4
gpt4 key购买 nike

我有一张图片,我使用 HTTP Post 方法将其上传到用 PHP 编写的网络服务器。我的代码将位图压缩为 jpeg 编码的字节数组,并将该 Base64 编码发送到解码的服务器,将其写入文件并将其作为 jpg 图像打开。

索引.php:

<?php
$base=$_REQUEST['image'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
?>

我在同一目录中有一个空的 0kb test.jpg。

有时我快速按F5(刷新)时可以看到图像,但是当我再次刷新时,图像又被删除并且又是0KB。 (在谷歌浏览器上工作)

1)如何让图片一直显示写入test.jpg的数据,不变回0kb?所以当我查看它时,图片仍然存在。 (请注意,我的 PHP 知识不是很好,因此非常感谢您的指导。)

JAVA、Android中的上传方法:

public void uploadFrame(byte[] Frame, String WebClient) {
String ba1=Base64.encodeBytes(mCurrentFrame);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));

try{

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost(WebClient);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}

2) 另一个问题是,这个方法是否真的安全,因为人们可以向它发送任何 POST 方法数据并显示任何图片。那么这在 PHP 中如何工作首先检查 JAVA 中的参数是否等于您的 PHP 代码中的参数,因此只有具有相同参数的人才能将此代码上传到他的网络服务器。

对于某些人来说,这可能是一个非常有趣的话题。

编辑:

<?php

if (isset($_REQUEST['image'])) {
$base=$_REQUEST['image'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
}

header('Content-type: image/jpeg');
readfile('test.jpg');
?>

最佳答案

每次刷新页面时,它都会将数据写入该 test.jpg 文件。如果您不提交 base64 图像,它只会写出一个空白/空字符串,这意味着一个 0 字节的文件。

相反,试试这个:

<?php

if (isset($_REQUEST['image'])) {
... your decoding/fopen/fwrite stuff here ...
}

header('Content-type: image/jpeg')
readfile('test.jpg');

这将直接将图像输出为 jpg,而不是输出 HTML 并强制浏览器打开另一个 HTTP 连接来加载 .jpg 文件。

关于java - PHP解码Base64,刷新并保留数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6510649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com