我做错了什么还是服务器有问题?这会挂起浏览器(MACOS 下的 Safari 和 Firefox):
<!DOCTYPE html>
<HTML>
<BODY>
<?php
$qq = str_repeat("X",409998);
echo $qq;
?>
</BODY>
</HTML>
运行在 Google 的 AppEngine、标准环境、PHP 8.2 上。如果我缩短字符串,效果很好。虽然问题是一致的,但如果我调整字符串的长度,我可以让它处理一些更长的字符串。
在解决问题之前,有一个解决方法:使用 Ajax 和 JSON 一次下载一个块文件,然后在 HTML 文件中重建该文件。将块大小设置为不会挂起浏览器的大小。
源.php
<?php
require '../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient();
$storage->registerStreamWrapper();
header("Access-Control-Allow-Origin: *");
$chunk_size = 300000; //multiple of 3 because base64 works with groups of 3 bytes
$chunk = $_REQUEST['chunk'];
if($chunk == 0) { // first request for file
$required_chunks = ceil(filesize("gs://$_REQUEST[key]") / $chunk_size);
if($required_chunks == 1) { // send complete file
$data="data:$_REQUEST[content_type];base64,".base64_encode(file_get_contents("gs://$_REQUEST[key]"));
echo json_encode(array($_REQUEST['target'],$data,0));
}
else { // file must be sent in chunks $data="data:$_REQUEST[content_type];base64,".base64_encode(file_get_contents("gs://$_REQUEST[key]",FALSE,NULL,0,$chunk_size));
echo json_encode(array($_REQUEST['target'],$data,1,$required_chunks,$_REQUEST['key']));
}
}
else { // continue sending chunks
$data=base64_encode(file_get_contents("gs://$_REQUEST[key]",FALSE,NULL,$chunk * $chunk_size,$chunk_size));
echo json_encode(array($_REQUEST['target'],$data,++$chunk,$_REQUEST['required_chunks'],$_REQUEST['key']));
}
}
?>
接收器.php
<!DOCTYPE html>
<HEAD>
<SCRIPT>
data_cache=[]
function getImage(target,type,k) {
var xmlhttp
xmlhttp=new XMLHttpRequest()
xmlhttp.onreadystatechange=function() {
if(xmlhttp.readyState==4 && xmlhttp.status==200) {
var d=JSON.parse(xmlhttp.responseText)
if(d[2] == 0) document.getElementById(d[0]).src=d[1] // single chunk
else {
if(d[2]!=d[3]) { // not the last chunk
if(d[2] == 1) data_cache[d[0]] = d[1] // start cache
else {
data_cache[d[0]] = data_cache[d[0]] + d[1] //append to cache
}
// send for next chunk
xmlhttp.open("GET","source.php?target="+d[0]+"&key="+d[4]+"&chunk="+d[2]+"&required_chunks="+d[3])
xmlhttp.send()
}
else {
dc = data_cache[d[0]] + d[1]
document.getElementById(d[0]).src = dc
}
}
}
}
xmlhttp.open("GET","source.php?target="+target+"&content_type="+type+"&key="+k+"&chunk=0")
xmlhttp.send()
}
</SCRIPT>
</HEAD>
<BODY>
<DIV><IMG id='i999'><SCRIPT>getImage('i999','image/jpeg',{bucket}/{file}.jpg')</SCRIPT></DIV>
</BODY>
</HTML>