PHP - 另一台服务器上的opendir [英] PHP - opendir on another server

查看:248
本文介绍了PHP - 另一台服务器上的opendir的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢PHP。



我有两个不同的主机,我想让我的php页面中的一个向我显示一个目录列表另一个。我知道如何在同一主机上使用opendir(),但是可以使用它来访问另一台机器吗?



提前感谢

解决方案

您可以使用PHP的 FTP功能远程连接到服务器并获取目录列表:

  //设置基本连接
$ conn_id = ftp_connect('otherserver.example.com');

//用用户名和密码登录
$ login_result = ftp_login($ conn_id,'username','password');

//检查连接
如果((!$ conn_id)||(!$ login_result)){
echoFTP连接失败!
退出;
}

//上传文件
$ upload = ftp_put($ conn_id,$ destination_file,$ source_file,FTP_BINARY);

//检查上传状态
如果(!$ upload){
echoFTP上传失败!
} else {
echo将$ source_file上传到$ ftp_server为$ destination_file;
}

//检索目录列表
$ files = ftp_nlist($ conn_id,'/ remote_dir');

//关闭FTP流
ftp_close($ conn_id);


I'm kinda new to PHP.

I've got two different hosts and I want my php page in one of them to show me a directory listing of the other. I know how to work with opendir() on the same host but is it possible to use it to get access to another machine?

Thanks in advance

解决方案

You could use PHP's FTP Capabilities to remotely connect to the server and get a directory listing:

// set up basic connection
$conn_id = ftp_connect('otherserver.example.com'); 

// login with username and password
$login_result = ftp_login($conn_id, 'username', 'password'); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    exit; 
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
    echo "FTP upload has failed!";
} else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// Retrieve directory listing
$files = ftp_nlist($conn_id, '/remote_dir');

// close the FTP stream 
ftp_close($conn_id);

这篇关于PHP - 另一台服务器上的opendir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆