[英]how to list all the directories of ftp server of iis using php for download
[英]Deleting all directories in an FTP archive that have a certain name using PHP
如果對服務器的唯一訪問是通過FTP,那么如何刪除目錄樹中所有具有特定名稱的目錄?
為了澄清,我想遍歷目錄樹,並通過FTP刪除名稱與某個字符串匹配的每個目錄。 在PHP中實現此方法的方法很好-我應該從哪里開始? 另外,如果有人知道已經可以執行此操作的任何實用程序,那也將是很好的。
這是一個起點,該功能將掃描FTP目錄並打印樹中與模式匹配的任何目錄的名稱。 我已經簡要測試過了。
function scan_ftp_dir($conn, $dir, $pattern) {
$files = ftp_nlist($conn, $dir);
if (!$files) {
return;
}
foreach ($files as $file) {
//the quickest way i can think of to check if is a directory
if (ftp_size($conn, $file) == -1) {
//get just the directory name
$dirName = substr($file, strrpos($file, '/') + 1);
if (preg_match($pattern, $dirName)) {
echo $file . ' matched pattern';
} else {
//directory didn't match pattern, recurse
scan_ftp_dir($conn, $file, $pattern);
}
}
}
}
然后做這樣的事情
$host = 'localhost';
$user = 'user';
$pass = 'pass';
if (false === ($conn = ftp_connect($host))) {
die ('cannot connect');
}
if (!ftp_login($conn, $user, $pass)) die ('cannot authenticate');
scan_ftp_dir($conn, '.', '/^beginswith/');
不幸的是,您只能使用ftp_rmdir()
刪除一個空目錄,但是如果您在此處查看 ,則有一個名為ftp_rmAll()
的函數可用於刪除找到的整個目錄結構。
另外,我僅在Unix上測試了使用ftp_size()
返回的失敗狀態作為檢查ftp_nlist()
返回的項是否為目錄的方法的技巧。
大概這個問題比最初出現的更多。
FTP支持DIR列出目錄內容,RMDIR刪除目錄和DEL刪除文件,因此它支持您需要的操作。
還是您在問如何遍歷FTP目錄樹?
您是否有首選/必需的實現語言?
好吧,這是我最終使用的腳本。 這是一個相當具體的實例,在該實例中必須使用它,但是如果您處在與我相同的困境中,只需輸入您的ftp服務器地址,用戶名,密碼,要刪除的文件夾的名稱以及要開始的文件夾,這將遍歷目錄,刪除所有與名稱匹配的文件夾。 如果與服務器的連接斷開,則重新連接存在一個錯誤,因此如果斷開連接,您可能需要再次運行腳本。
if( $argc == 2 ) {
$directoryToSearch = $argv[1];
$host = '';
$username = '';
$password = '';
$connection = connect( $host, $username, $password );
deleteDirectoriesWithName( $connection, 'directoryToDelete', $directoryToSearch );
ftp_close( $connection );
exit( 0 );
}
else {
cliPrint( "This script currently only supports 1 argument.\n");
cliPrint( "Usage: php deleteDirectories.php directoryNameToSearch\n");
exit( 1 );
}
/**
* Recursively traverse directories and files starting with the path
* passed in and then delete all directories that match the name
* passed in
* @param $connection the connection resource to the database.
* @param $name the name of the directories that should be * deleted.
* @param $path the path to start searching from
*/
function deleteDirectoriesWithName( &$connection, $name, $path ) {
global $host, $username, $password;
cliPrint( "At path: $path\n" );
//Get a list of files in the directory
$list = ftp_nlist( $connection, $path );
if ( empty( $list ) ) {
$rawList = ftp_rawlist( $connection, $path );
if( empty( $rawList ) ) {
cliPrint( "Reconnecting\n");
ftp_close( $connection );
$connection = connect( $host, $username, $password );
cliPrint( "Reconnected\n" );
deleteDirectoriesWithName( $connection, $name, $path );
return true;
}
$pathToPass = addSlashToEnd( $path );
$list = RawlistToNlist( $rawList, $pathToPass );
}
//If we have selected a directory, then 'visit' the files (or directories) in the dir
if ( $list[0] != $path ) {
$path = addSlashToEnd( $path );
//iterate through all of the items listed in the directory
foreach ( $list as $item ) {
//if the directory matches the name to be deleted, delete it recursively
if ( $item == $name ) {
DeleteDirRecursive( $connection, $path . $item );
}
//otherwise continue traversing
else if ( $item != '..' && $item != '.' ) {
deleteDirectoriesWithName( $connection, $name, $path . $item );
}
}
}
return true;
}
/**
*Put output to STDOUT
*/
function cliPrint( $string ) {
fwrite( STDOUT, $string );
}
/**
*Connect to the ftp server
*/
function connect( $host, $username, $password ) {
$connection = ftp_connect( $host );
if ( !$connection ) {
die('Could not connect to server: ' . $host );
}
$loginSuccessful = ftp_login( $connection, $username, $password );
if ( !$loginSuccessful ) {
die( 'Could not login as: ' . $username . '@' . $host );
}
cliPrint( "Connection successful\n" );
return $connection;
}
/**
* Delete the provided directory and all its contents from the FTP-server.
*
* @param string $path Path to the directory on the FTP-server relative to
* the current working directory
*/
function DeleteDirRecursive(&$resource, $path) {
global $host, $username, $password;
cliPrint( $path . "\n" );
$result_message = "";
//Get a list of files and directories in the current directory
$list = ftp_nlist($resource, $path);
if ( empty($list) ) {
$listToPass = ftp_rawlist( $resource, $path );
if ( empty( $listToPass ) ) {
cliPrint( "Reconnecting\n" );
ftp_close( $resource );
$resource = connect( $host, $username, $password );
$result_message = "Reconnected\n";
cliPrint( "Reconnected\n" );
$result_message .= DeleteDirRecursive( $resource, $path );
return $result_message;
}
$list = RawlistToNlist( $listToPass, addSlashToEnd( $path ) );
}
//if the current path is a directory, recursively delete the file within and then
//delete the empty directory
if ($list[0] != $path) {
$path = addSlashToEnd( $path );
foreach ($list as $item) {
if ($item != ".." && $item != ".") {
$result_message .= DeleteDirRecursive($resource, $path . $item);
}
}
cliPrint( 'Delete: ' . $path . "\n" );
if (ftp_rmdir ($resource, $path)) {
cliPrint( "Successfully deleted $path\n" );
} else {
cliPrint( "There was a problem deleting $path\n" );
}
}
//otherwise delete the file
else {
cliPrint( 'Delete file: ' . $path . "\n" );
if (ftp_delete ($resource, $path)) {
cliPrint( "Successfully deleted $path\n" );
} else {
cliPrint( "There was a problem deleting $path\n" );
}
}
return $result_message;
}
/**
* Convert a result from ftp_rawlist() to a result of ftp_nlist()
*
* @param array $rawlist Result from ftp_rawlist();
* @param string $path Path to the directory on the FTP-server relative
* to the current working directory
* @return array An array with the paths of the files in the directory
*/
function RawlistToNlist($rawlist, $path) {
$array = array();
foreach ($rawlist as $item) {
$filename = trim(substr($item, 55, strlen($item) - 55));
if ($filename != "." || $filename != "..") {
$array[] = $filename;
}
}
return $array;
}
/**
*Adds a '/' to the end of the path if it is not already present.
*/
function addSlashToEnd( $path ) {
$endOfPath = substr( $path, strlen( $path ) - 1, 1 );
if( $endOfPath == '/' ) {
$pathEnding = '';
}
else {
$pathEnding = '/';
}
return $path . $pathEnding;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.