2014-04-17 51 views
1

我開始使用ftp項目,並且無法重新加載目錄。我的代碼說它已成功更改,但我認爲它只會改變服務器端。我需要瀏覽器來更改目錄。PHP ftp_chdir不可見地更改目錄

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

解決!新增了一個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>"; 
    } 
}