在 AJAX 调用之间保留 FTP 连接 PHP 资源

发布于 2024-10-08 12:27:05 字数 335 浏览 0 评论 0原文

我有一个多用户 PHP Web 应用程序,可以通过 AJAX 与 FTP 服务器交互。该应用程序允许用户浏览 FTP 站点。 JavaScript 进行 AJAX 调用,该调用与服务器脚本通信,返回给定目录中的文件和目录列表。

这很好用。然而,每次请求目录列表时,服务器都必须重新建立与FTP服务器的连接,这需要花费大量时间。

我需要在 AJAX 调用之间保留 FTP 连接 PHP 资源。换句话说,连接必须保持打开状态,并且我必须能够使用该资源运行 ftp_nlist(),而无需在每次新的 AJAX 调用时重新建立连接或重新进行身份验证(当然,直到连接超时) 。

任何人都可以想出一种方法来做到这一点吗?

I have a multi-user PHP web application that can interact with an FTP server via AJAX. The application allows the user to browse an FTP site. Javascript makes an AJAX call which communicates with a server-script that returns a list of files and directories within a given directory.

This works fine. However, each time a directory listing is requested, the server must re-establish a connection with the FTP server, which takes a lot of time.

I need to persist an FTP connection PHP resource across AJAX calls. In other words, the connection must remain open, and I must be able to run ftp_nlist() using that resource, without re-establishing the connection or re-authenticating, with each new AJAX call (until the connection times out, of course).

Can anyone think of a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

平定天下 2024-10-15 12:27:05

我认为不可能在 PHP 中使用 FTP 库。我看到有人甚至在 PHP 中提出了功能请求,但似乎没有采取任何行动。

我能想到的唯一方法是使用第 3 方 FTP 客户端,该客户端保持连接打开并通过 PHP 与其交互。 (您可以只使用操作系统内置的 FTP 功能,而不是使用第 3 方 ftp 客户端。Windows 提供了这些功能,Linux 也通过“ftp”程序提供了这些功能。)

I don't think it's possible using the FTP library in PHP. I see somebody even had a feature request for it in PHP, but it doesnt seem there was any action taken on it.

The only way I can think of is to use a 3rd-party FTP client that keeps the connection open and interface with it through PHP. (instead of a 3rd party ftp client, you could just use the FTP functions built-in to the OS. Windows provides them, as does Linux through the "ftp" program.)

溺ぐ爱和你が 2024-10-15 12:27:05

很抱歉在没有给你明确答案的情况下增加混乱,但这可能会有所帮助:
http://www.eecho.info/Echo/php/客户端-url-library-php-curl/
看来您可以控制打开和关闭连接,但是就将此变量返回给客户端并重新使用它而言,我不确定这是否可能(而且它可能会在您的控制之外自行清理),或者,您可以(取决于最终环境)考虑使用 Java 后端,您可以编写一个简单的服务器,然后在顶部添加 FTP 代码(嗯……蛋糕)。您需要为此执行的一些示例如下:

http://fragments.turtlemeat.com/ javawebserver.php

http://www.javawebserver.php javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html

这假设对服务器环境中运行的内容有相当大的控制权,但实际上取决于您基本上拥有服务器或拥有服务器拥有做你想做的事情的全部权限(至少像他们广告中的 Amazon EC2 一样)。您也许可以使用 Tomcat 或其他一些 JSP 容器来实现这一点,并使用 JSP 而不是编写自己的服务器,但我不知道您是否能够保持连接,因为它与 PHP 有点相同可以说,服务器通常“即时”解释文件。

Sorry to add clutter without a clear answer for ya but this might be helpful:
http://www.eecho.info/Echo/php/client-url-library-php-curl/
It appears you are in control of opening and closing the connections however in terms of returning this variable to the client and having it re-used I'm not sure that is possible (also it might just clean itself up out of your control), alternatively you might (depending on the end environment) consider using a Java backend, you could code up a simple server and just add the FTP code on top (mmm... cake). Some examples of what you'd need to do for that are here:

http://fragments.turtlemeat.com/javawebserver.php

http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html

This assumes a pretty large amount of control of what's run in the server environment though so really depends on you owning the server basically or having full priveleges to do do what you want (like Amazon EC2 from what they advertise at least). You might be able to pull this off with Tomcat or some other JSP container and use JSPs instead of writing your own server but I don't know that you'd be able to persist the connection their either since it's sort of the same as PHP where the server generally interprets the file "on the fly" so to speak.

稳稳的幸福 2024-10-15 12:27:05

您无法使用 PHP 的普通 ftp 类函数创建持久的 FTP 连接。所有用户是否都访问同一个 ftp 服务器,或者您是否正在运行 ftp Web 界面?如果多个用户连接到同一服务器(具有相同的权限),您可以实施缓存解决方案。

You can not create a persistent FTP connection with PHPs normal ftp classes functions. Are all users accesing the same ftp server or are you running a ftp web interface? If multiple users are connected to the same server (with the same rights) you could implement a cached solution.

葬シ愛 2024-10-15 12:27:05

我最终使用全局变量(例如 $my_global)来完成这项工作。我有一个 ConnectionPooler 单例类,它管理存储在哈希中的连接。

I ended up making this work using global variables (eg. $my_global). I have a ConnectionPooler singleton class which manages connections stored in a hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文