开发者

Delete files on a FTP server with the word "ub" in their filenames

开发者 https://www.devze.com 2023-04-05 22:00 出处:网络
I connect via ftp_connect and ftp_login to a FTP server. Once connected, I go to a directory with ftp_chdir. In the directory, I have to delete with ftp_delete all files that have the word \"ub\" i开发

I connect via ftp_connect and ftp_login to a FTP server. Once connected, I go to a directory with ftp_chdir. In the directory, I have to delete with ftp_delete all files that have the word "ub" i开发者_开发技巧n their filenames. So I have to read somehow every filename and delete only those files who have "ub" in their filenames. I have no idea how to do this. Please help. Thanks.


If you use an interactive ftp command-line tool, you can issue the command

mdel *ub*

but the low-level protocol doesn't support wildcard operations; this is something that has to be implemented in the client by fetching all the names, comparing against the pattern, and deleting one-by-one, as you said. You might want to consider scripting this using command-line ftp, rather than using php?


@Pekka's comment has one possible solution. Another is using glob.

$files = glob('*ub*');


foreach (glob("*ub*") as $file) {
    ftp_delete('YOUR_CONNECTION', $file);
}

regards


Since there is no real answer to this question, I will answer with my functions that allows to delete multiple files over ftp:

/**
 * Delete multiple files on FTP server. Allowed wildcards are * and ?.
 * @param resource $ftp_connection
 * @param string $delete_pattern
 * @param bool $case_sensitive Case sensitivity is by default
 * @return bool|int Number of deleted files, FALSE on failure
 */
function ftp_mdelete($ftp_connection, $delete_pattern = "", $case_sensitive = TRUE){
    if(!is_resource($ftp_connection) || strtolower(get_resource_type($ftp_connection)) !== "ftp buffer"){
        trigger_error("First parameter for ftp_mdelete should be a valid FTP connection", E_USER_WARNING);
        return FALSE;
    }elseif(!is_string($delete_pattern) || !strlen($delete_pattern)){
        trigger_error("Second parameter for ftp_mdelete should be a non-empty string", E_USER_WARNING);
        return FALSE;
    }
    $raw_list = ftp_rawlist($ftp_connection, '.');
    if(!is_array($raw_list)){
        return FALSE;
    }
    $matched_count = 0;
    $deleted_count = 0;
    if($raw_list){
        $delete_pattern = preg_quote($delete_pattern);
        $delete_pattern = '/^'.str_replace(array('\*', '\?'), array('.*', '.'), $delete_pattern).'/S'.($case_sensitive?'':'i');
        foreach($raw_list as $entry){
            if($entry{0} === '-'){
                $entry = preg_split("/[\s]+/S", $entry, 9);
                $entry = $entry[8];
                if(preg_match($delete_pattern, $entry)){
                    ++$matched_count;
                    if(ftp_delete($ftp_connection, $entry)){
                        ++$deleted_count;
                    }
                }
            }
        }
        unset($raw_list, $entry);
    }
    if($matched_count != $deleted_count && $deleted_count){
        trigger_error("Only {$deleted_count} out of {$matched_count} files deleted.", E_USER_NOTICE);
    }elseif($matched_count && !$deleted_count){
        trigger_error("No files were deleted ({$matched_count} files matched given pattern).", E_USER_WARNING);
        return FALSE;
    }
    return $deleted_count;
}

Usage example:

$ftp = ftp_connect('127.0.0.1');
ftp_login($ftp, 'user', 'pass');
ftp_chdir($ftp, 'dir');
$deleted = ftp_mdelete($ftp, '*ub*');
ftp_close($ftp);

echo "Number of deleted files: ".intval($deleted);
0

精彩评论

暂无评论...
验证码 换一张
取 消