PHP ftp_chdir没有明显改变目录

时间:2014-04-17 16:58:27

标签: php ftp

我开始使用ftp项目,但我无法重新加载目录。我的代码说它成功改变了,但我认为它只会改变服务器端。我还需要浏览器来更改目录。

PS:如何通过浏览器通过FTP下载?当我在本地测试时,它会将文件写入根目录,但是当远程连接时,我不知道它们的去向。没有任何文件被下载的迹象。

任何帮助将不胜感激!如果你对我有任何提示,那就太好了。我现在还很新,但我正在努力。

<?php
session_id('logon');
session_start();

if (isset($_POST['connect']))
{
$_SESSION['port'] = $_POST['port'];
$_SESSION['server'] = $_POST['server'];
$_SESSION['user'] = $_POST['user'];
$_SESSION['password'] = $_POST['password'];
}
$port = $_SESSION['port'];
$server = $_SESSION['server'];
$user = $_SESSION['user'];
$pass = $_SESSION['password'];

$connection = ftp_connect($server)
or die("Couldn't connect!");

$logon = ftp_login($connection,$user,$pass)
or die("Couldn't login!" . $server ."<br>". $port);


$workingDir = ftp_pwd($connection);

echo "You are in $workingDir<br><br>";

$dirList = ftp_nlist($connection, ".");

foreach($dirList as $item)
{
    $res = ftp_size($connection, $item);
    if ($res != "-1")
    {
        echo "<a href='?download=$item'>$item</a><br>";
        if (isset($_GET['download']))
        {
            if ($_GET['download'] == $item)
            {
                include('include/download.php');
            }
        }
    }
    else 
    {
            $directory = $item;
            echo "<a href='?change=$directory'>$directory</a><br>"; 
            if ($_GET['change'] == $directory)
            {
                    if (ftp_chdir($connection, $directory))
                    {
                        echo "Changed to " . ftp_pwd($connection) . "!<br>";
                        $dirList = ftp_nlist($connection, ".");
                        header("Refresh:0");
                    }
                    else
                    {
                        echo "Failed to change to $directory";
                    }

            }       
    }
}


ini_set('error_reporting', E_ALL);
ftp_quit($connection);
?>

1 个答案:

答案 0 :(得分:1)

解决!添加了带

的directory.php文件
$getChange = $_GET['change'];

if (ftp_chdir($connection, $getChange))
{
    echo "Changed to " . ftp_pwd($connection) . "!<br>";
    $dirList = ftp_nlist($connection, ".");
}
else
{
    echo "Failed to change to $getChange";
}

$workingDir = ftp_pwd($connection);
echo "You are in $workingDir<br><br>";  

$dirList = ftp_nlist($connection, ".");

foreach($dirList as $item)
{
    $res = ftp_size($connection, $item);

    if ($res != "-1")
    {
        echo "<a href='?download=$item'>$item</a><br>";

        if (isset($_GET['download']))
        {
            if ($_GET['download'] == $item)
            {
                include('include/download.php');
            }
        }
    }
    else 
    {
        echo "<a href='directory.php?change=$item'>$item</a><br>";  
    }
}