简体   繁体   English

PHP FTP和ajax或类似版本

[英]PHP FTP and ajax or similar

I have written a PHP class that allows a user to interact with an FTP server, connect, change directory, upload, etc. What I am wanting to do next is to give the realtime updates of what is happening on the server, ie. 我编写了一个PHP类,该类允许用户与FTP服务器进行交互,连接,更改目录,上传等。下一步,我想对服务器上发生的事情进行实时更新。 if they change directory, throw them a message saying directory changed you are now in {pwd}. 如果他们更改了目录,请向他们发送一条消息,指出目录已更改,您现在位于{pwd}中。

Is this even possible? 这有可能吗? I am happy with my PHP class, but I have no idea how to give it the realtime aspect. 我对我的PHP类感到满意,但是我不知道如何提供实时性。

What you are describing is predominantly a frontend engineering question. 您所描述的主要是前端工程问题。

Typically in a browser based UI, these kind of "realtime" situations are simulated with ajax calls that are made to the server at a timed interval using JS. 通常,在基于浏览器的UI中,这种“实时”情况是通过使用JS在定时间隔对服务器进行的Ajax调用来模拟的。 You can set up endpoints that return representations of your "answer" to the ping and update your frontend display with the relevant info. 您可以设置终结点,以将“答案”的表示形式返回给ping,并使用相关信息更新前端显示。 This circumvents the stateless nature of the HTTP protocol. 这规避了HTTP协议的无状态性质。

I would recommend any calls made in this "pinging" manner also update their interval in the event that they receive no new data. 我建议以这种“ ping”方式进行的任何呼叫也应在没有收到新数据的情况下更新其间隔。 Its a lot more efficient to check less often when several consecutive responses show no new information. 当多个连续响应未显示新信息时,检查频率降低效率更高。 You can increase the interval every time there is nothing new to report. 每次没有新要报告时,您可以增加间隔。

There are more advanced implementations when true "realtime" is required, you can google "HTTP Push" for more information on how to hang an HTTP response until new data is sent. 当需要真正的“实时”时,还有一些更高级的实现,您可以在“ HTTP Push”上搜索有关如何挂起HTTP响应直到发送新数据的更多信息。 I don't think anything this particular would be required in your case however. 我认为,在您的情况下,不需要任何特殊说明。

to simulate realtime updates, you can write a simple javascript code that checks for changes on the server at a time interval. 为了模拟实时更新,您可以编写一个简单的javascript代码,该代码每隔一段时间检查服务器上的更改。 Like a Ping, just return if something new exists or not. 像Ping一样,只要有新东西存在,就返回。 if the server returns true, you can fetch all messages. 如果服务器返回true,则可以获取所有消息。

other approaches, that are getting used more and more are websockets. Websocket是其他越来越被使用的方法。 http://socket.io/ is a helpful example. http://socket.io/是一个有用的示例。 It is based on node.js 它基于node.js

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

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