承接上篇,本文實作 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++;
}
}
?>
<center>
You are currently working in <b><?php echo $here; ?></b><br>
<?php echo $status; ?>
</center>
<hr>
<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>
<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>
<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>
ftp_chdir($result, $cdir . "/" . $rdir),首次無 $rdir 則用 .$dfile[],ftp_delete($result, $cdir . "/" . $file)ftp_get($result, $local, $remote, FTP_BINARY),注意指定不同本地路徑避免覆蓋ftp_put($result, $upfile_name, $upfile, FTP_BINARY),建議改用 $_FILES['upfile']['tmp_name'] 更安全ftp_size() 逐個呼叫極慢,大目錄需快取或改用 ftp_rawlist() 解析ftp_error() 類封裝