简体   繁体   English

创建zip存档并将其流式传输到ftp服务器

[英]Create zip archive and stream it to ftp server

I am trying to create a zip file from local files and stream it directly to an ftp (without writing the zip to disk first). 我正在尝试从本地文件创建一个zip文件,并将其直接流式传输到ftp(而无需先将zip写入磁盘)。 I have no problems with zipping itself but it seems like the ZipArchive class doesn't recognize the ftp stream wrapper. 我自己没有压缩问题,但似乎ZipArchive类无法识别ftp流包装器。

The following code is the simplest thing I could come up with that will illustrate the problem 以下代码是我能想到的最简单的方法,它将说明问题

<?php

$zip = new ZipArchive();
var_dump($zip->open('ftp://[username]:[password]@[hostname.net]/public_html/test.zip', ZipArchive::OVERWRITE));

$zip->addFile(realpath('/input.txt'), 'input.txt');

var_dump($zip->close());

The $zip->open call returns true while $zip->close returns false . $zip->open调用返回true$zip->close调用返回false I can't find a way to get an error message or something that can be more specific than just there is an error . 我找不到一条错误消息的方法,或可能比there is an error更具体there is an error The question is what am I doing wrong, or I can't do these kind of stuff with the ZipArchive class. 问题是我做错了什么,或者我不能使用ZipArchive类来做这些事情。

I've tested your code on my Ubuntu system and I can confirm that it does'nt work. 我已经在Ubuntu系统上测试了您的代码,并且可以确认它不起作用。
By using the "strace" utility, I've seen that the PHP is handling the "$filename" argument as if it was a local file name, as you can see below: 通过使用“ strace”实用程序,我已经看到PHP正在像对待本地文件名一样处理“ $ filename”自变量,如下所示:

lstat64("/var/www/test/ftp://myuser:mypass@127.0.0.1/tmp/test.zip", 0xbfcbb008) = -1 ENOENT (No such file or directory)
lstat64("/var/www/test/ftp://myuser:mypass@127.0.0.1/tmp", 0xbfcbaeb8) = -1 ENOENT (No such file or directory)
lstat64("/var/www/test/ftp://myuser:mypass@127.0.0.1", 0xbfcbad68) = -1 ENOENT (No such file or directory)
lstat64("/var/www/test/ftp:", 0xbfcbac28) = -1 ENOENT (No such file or directory)

The "/var/www/test" prefix is the path of my PHP test script. “ / var / www / test”前缀是我的PHP测试脚本的路径。
So, it seems that effectively the ZipArchive::open() method cannot handle URL filenames. 因此,似乎ZipArchive :: open()方法实际上无法处理URL文件名。
Looking at the PHP source code, and I've discovered that the ZipArchive::open() method calls the " zip_open " function. 通过查看PHP源代码,我发现ZipArchive :: open()方法调用了“ zip_open ”函数。
The PHP manual page for the " fopen " function states: PHP手册页中的“ fopen ”函数指出:

If filename is of the form "scheme://...", it is assumed to be a URL and PHP will search for a protocol handler (also known as a wrapper) for that scheme. 如果文件名的格式为“ scheme:// ...”,则假定它是一个URL,PHP将为该方案搜索协议处理程序(也称为包装器)。 If no wrappers for that protocol are registered, PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file. 如果没有注册该协议的包装器,PHP将发出一条通知,以帮助您跟踪脚本中的潜在问题,然后继续执行,就好像文件名指定了常规文件一样。

This post , found in the PHP manual page for the " stream_wrapper_register " function, states that zip_open() ignores custom stream wrappers, but the above test shows that it also ignores standard stream wrappers. 该帖子在PHP手册页的“ stream_wrapper_register ”函数中找到,指出zip_open()忽略自定义流包装器,但上述测试表明它也忽略了标准流包装器。
It's also true that neither ZipArchive::open() manual page nor the zip_open manual page explicitly say that the "$filename" parameter can be a URL. 确实, ZipArchive :: open()手册页和zip_open手册页均未明确指出“ $ filename”参数可以是URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM