【问题标题】:Using php to show a file on ftp server使用 php 在 ftp 服务器上显示文件
【发布时间】:2013-11-04 11:52:06
【问题描述】:

我有一个 ftp 服务器,其中有一个名为“file.txt”的文件。
在我的网站上,我想使用 php 显示此文件包含的内容。
我得到了什么:

<?php
session_start();


$_SESSION["ftp_pass"] = $_POST["pass"];
$_SESSION["ftp_user"] = $_POST["user"];
$ftp_pass = $_POST["pass"];
$ftp_server = "ftp.guusvanwalstijn.nl";

$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 


// Error when wrong password/username/server offline
function error_while_connecting() {
echo "Error while connecting to the server";
echo "<br />Probably the password is wrong or the server is offline";
}

//log in
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "succes";
} else {
    error_while_connecting();
    print("<br />Debug: " . $_POST["pass"] . "<br />");
}

ftp_close($conn_id);
?>

【问题讨论】:

    标签: php file ftp


    【解决方案1】:

    这样做:

     <?php
    $file = $_SERVER['DOCUMENT_ROOT'] . "/text.txt"; //Path to your *.txt file 
    $contents = file($file); 
    $string = implode($contents); 
    
    echo $string; 
    ?>
    

    【讨论】:

    • 我所做的是:function test() { $file = $_SERVER['DOCUMENT_ROOT'] 。 "/file.txt"; //*.txt 文件的路径 $contents = file($file); $string = implode($contents);回声$字符串;但发生的事情是它试图从我的虚拟主机打开一个名为“file.txt”的文件,因为在我的虚拟主机中创建 file.txt 时,它确实显示了这个文件中的所有内容
    • 啊,没关系,我将 $_SERVER 更改为 $ftp_server。非常感谢