首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP FTP 客戶端實作教學(二):介面層與目錄/檔案操作完整代碼

     編輯:PHP 綜合知識

系列回顧

承接上篇,本文實作 include.php:負責取得當前目錄列表、區分檔案/子目錄、產生三大表單(切換目錄、檔案操作、上傳檔案)。

核心邏輯:掃描目錄並分類

<?php
// 取得當前路徑
$here = ftp_pwd($result);

// 初始化三個陣列
$files = array();        // 檔案名
$file_sizes = array();   // 對應大小
$dirs = array();         // 子目錄名

$file_list_counter = 0;
$dir_list_counter = 0;

// ftp_nlist 回傳所有項目(檔+目錄)
$filelist = ftp_nlist($result, ".");

foreach ($filelist as $item) {
    // ftp_size 檔案回傳位元組數,目錄回傳 -1
    $size = ftp_size($result, $item);
    if ($size != -1) {
        $files[$file_list_counter] = $item;
        $file_sizes[$file_list_counter] = $size;
        $file_list_counter++;
    } else {
        $dirs[$dir_list_counter] = $item;
        $dir_list_counter++;
    }
}
?>

介面三大區塊

1. 當前路徑 + 狀態訊息

<center>
You are currently working in <b><?php echo $here; ?></b><br>
<?php echo $status; ?>
</center>
<hr>

2. 目錄下拉選單(切換目錄)

<form action="actions.php" method="post">
<input type="hidden" name="username" value="<?php echo $username; ?>">
<input type="hidden" name="password" value="<?php echo $password; ?>">
<input type="hidden" name="server" value="<?php echo $server; ?>">
<input type="hidden" name="cdir" value="<?php echo $here; ?>">
<input type="hidden" name="action" value="CWD">

<select name="rdir">
<option value=".."><parent directory></option>
<?php foreach ($dirs as $d) echo "<option value='$d'>$d</option>"; ?>
</select>
<input type="submit" value="Go">
</form>
<hr>

3. 檔案清單 + 勾選框(刪除/下載)

<form action="actions.php" method="post">
<input type="hidden" name="server" value="<?php echo $server; ?>">
<input type="hidden" name="username" value="<?php echo $username; ?>">
<input type="hidden" name="password" value="<?php echo $password; ?>">
<input type="hidden" name="cdir" value="<?php echo $here; ?>">

<table border="0" width="100%">
<?php foreach ($files as $i => $f) {
    echo "<tr><td><input type='checkbox' name='dfile[]' value='$f'> $f <i>(".$file_sizes[$i]." bytes)</i></td></tr>";
} ?>
</table>

<center>
<input type="submit" name="action" value="Delete">
<input type="submit" name="action" value="Download">
</center>
</form>
<p><hr>

4. 檔案上傳表單

<form enctype="multipart/form-data" action="actions.php" method="post">
<input type="hidden" name="username" value="<?php echo $username; ?>">
<input type="hidden" name="password" value="<?php echo $password; ?>">
<input type="hidden" name="server" value="<?php echo $server; ?>">
<input type="hidden" name="cdir" value="<?php echo $here; ?>">

<table>
<tr><td><input type="file" name="upfile"></td></tr>
<tr><td><input type="submit" name="action" value="Upload"></td></tr>
</table>
</form>

actions.php 關鍵處理邏輯(摘要)

  • CWD:ftp_chdir($result, $cdir . "/" . $rdir),首次無 $rdir 則用 .
  • Delete:遍歷 $dfile[],ftp_delete($result, $cdir . "/" . $file)
  • Download:ftp_get($result, $local, $remote, FTP_BINARY),注意指定不同本地路徑避免覆蓋
  • Upload:ftp_put($result, $upfile_name, $upfile, FTP_BINARY),建議改用 $_FILES['upfile']['tmp_name'] 更安全

安全性與改進建議

  • 隱藏欄位傳遞帳密,建議改用 Session 儲存連線資源
  • 無 SSL/TLS 加密,生產環境必須用 FTPS 或 SFTP
  • ftp_size() 逐個呼叫極慢,大目錄需快取或改用 ftp_rawlist() 解析
  • 下載預設存當前目錄,易覆蓋腳本,需指定專用下載資料夾
  • 錯誤處理僅靠回傳值,建議加入 ftp_error() 類封裝
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved