我有一个带有我的数据库的处理服务器和一个服务数据库,可以以较低的带宽成本提供文件.在处理服务器上,php 无法创建文件,因此在发送到另一台服务器进行下载之前,必须使用流完成所有操作和/或保留在内存中.几天前,我发现了使用 'php://memory' 的流抽象,我可以做类似
I have a processing server with my database and a serving database to serve up files with a low bandwidth cost. On the processing server, php is not able to create files so everything must be done with streams and/or stay in memory before being sent over to another server for download. A few days ago I found out about the stream abstraction with 'php://memory' and that I can do something like
$fp=fopen('php://memory','w+');
fwrite($fp,"Hello world");
fseek($fp,0,SEEK_SET);
//make a ftp connection here with $conn_id
$upload = ftp_fput($conn_id,"targetpath/helloworld.txt",$fp,FTP_BINARY);
在内存中制作文件,然后允许我将它通过 ftp 传输到我的其他服务器.这正是我想要的,除了我还想在发送数据之前压缩数据——最好只使用 php 的本机部分,如 ziparchive,而不是额外的自定义类来进行特殊的流操作.我知道我非常接近以下...
to make the file in memory and then allow me to ftp it over to my other server. This is exactly what I want, except I also want to zip the data before sending it -- preferably using only native parts of php like ziparchive and not additional custom classes for special stream manipulation. I know that I am very close with the following...
$zip = new ZipArchive();
if($zip->open('php://memory', ZIPARCHIVE::CREATE)) {
$zip->addFromString('testtext.txt','Hello World!');
$fp = $zip->getStream('test'); if(!$fp) print "no filepointer";
//make a ftp connection here with $conn_id
$upload = ftp_fput($conn_id,"targetpath/helloworld.zip",$fp,FTP_BINARY);
} else print "couldn't open a zip like that";
失败的点是对 getStream 的调用(尽管我认为我使用正确,但它总是返回 false).看起来 zip 可以在php://memory"中制作文件,但由于某种原因 getStream 仍然失败,尽管我可能不太了解 ZipArchive 如何制作 zip...
The point at which this fails is the call to getStream (which always returns false although I think I am using correctly). It appears that the zip is fine making the file in 'php://memory' but for some reason getStream still fails although perhaps I don't sufficiently understand how ZipArchive makes zips...
我怎样才能从字符串到压缩文件指针,以便我可以通过 ftp 将 zip 文件传送到我的其他服务器?请记住,我无法制作任何文件,否则我只会制作 zip 文件,然后通过 ftp 将其复制.
How can I go from the string to the zipped filepointer so that I can ftp the zip over to my other server? Remember I can't make any files or else I would just make the zip file then ftp it over.
根据下面瘦子的建议,我尝试了以下操作
based on skinnynerd's suggestions below I tried the following
$zip = new ZipArchive();
if($zip->open('php://memory', ZIPARCHIVE::CREATE)) {
$zip->addFromString('testtext.txt','Hello World!');
$zip->close();
$fp = fopen('php://memory','r+');
fseek($fp,0,SEEK_SET);
//connect to ftp
$upload = ftp_fput($conn_id,"upload/transfer/helloworld.zip",$fp,FTP_BINARY);
}
这确实制作了一个 zip 并将其发送过来,但 zip 是 0 字节大,所以我不认为 'php://memory' 像我想的那样工作......它实际上在关闭步骤中失败了——$zip->close() 返回 false,这让我想知道我是否可以将 zips 打开到 'php://memory' 中.有谁知道我可以按照这些方法尝试获取 zip 文件吗?
This does make a zip and send it over but the zip is 0 bytes large so I don't think that 'php://memory' works the way I thought... it actually fails at the close step -- the $zip->close() returns false which makes me wonder if I can open zips into 'php://memory' at all. Does anyone know what I can try along these line to get the zip?
它必须是 Zip 存档吗?由于您正在尝试节省带宽,因此它也可能是 gzip.
Does it have to be a Zip archive? Since you're trying to save bandwith it could be a gzip too.
<?php
$ftp_credentials = "ftp://USER:PASSWORD@HOST/helloworld.gz";
$gz = gzencode("Hello World!", 9);
$options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($options);
file_put_contents($ftp_credentials, $gz, 0, $stream_context);
?>
这篇关于字符串到 php 中的压缩流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!