PHP - 从 URL 下载并通过 FTP 上传
这里的概念有点奇怪......我们的客户希望通过 FTP/S 将数据推送给他们。
这个想法是,我们通过从 URL(CSV 文件)下载来下载一份报告,然后通过 FTP/将其推送到客户端S。我知道我可以使用 wget & 在 bash 脚本中执行此操作ftp - 但需要通过 Web 界面添加此内容,因此 PHP 是最好的方法。
由于这是一项后台任务,我可以延长超时等。
我还知道我可以使用 fopen 下载并保存文件,然后找到它并使用 PHP FTP 库上传它。只是寻找一种使用 fopen 下载的方法,将数据保存在内存中以便立即上传。
如有任何帮助,请提前表示感谢!
Slightly weird concept here... A client of ours wants data pushed to them over FTP/S
The idea is that we download one of our reports by downloading from a URL (a CSV File), then push this to the client over FTP/S. I know I can do this in bash scripts using wget & ftp - but need to add to this over a web interface so PHP is the best way forward.
As this is a background task I can extend time-outs etc.
I know also I can use fopen to download and save a file, then find it and upload it using the PHP FTP library. Just looking for a way to download using fopen, hold the data in memory to upload straight away.
Any help appreciated in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要从 URL 检索数据,您有几种选择。你说你只想将内存中的数据直接推送到FTP主机。
一种方法(我发现最简单,但缺乏可靠性和错误处理)是
file_get_contents()
示例:
现在您的 csv 数据位于
$data
中,关于如何将其推送到 ftp 服务器。同样,最简单的方法是使用上面 get 示例中使用的内置流包装器。 (但请注意,这需要 PHP 4.3.0)
只需像这样构建连接字符串即可。
查看 ftp 包装器手册
如果这不起作用,有其他选项。
您可以使用 curl 来获取数据和 FTP 扩展 来推送它。
To retrieve the data from the URL you have a few options. You say you want the data in memory only to push directly to the FTP host.
One approach (that I find the simplest to use, but lacking in terms of reliability and error handling) is
file_get_contents()
Example:
Now you have your csv data in
$data
, over to how to push this to an ftp server.Again the simplest way to do this is to use the builtin stream wrappers as used in the get example above. (Note however that this requires PHP 4.3.0)
Simply build up the connection string like this.
Have a look at the ftp wrappers manual
If this does not work there are other options.
You could use curl to get the data and the FTP Extension to push it.
为了避免将文件保存到磁盘并“立即上传”,即在下载第一个数据块后立即开始推送到 FTP?
试试这个:
http://www.php.net/manual/en /function.stream-copy-to-stream.php
您需要一个支持恢复上传的 FTP 服务器和客户端库
To avoid saving the file to disk and "to upload straight away" i.e. to start pushing to FTP as soon as the first chunk of Data is downloaded?
Try this:
http://www.php.net/manual/en/function.stream-copy-to-stream.php
You'll need an FTP server and client library which support resuming uploads