Sample code for 30+ languages & platforms
PHP

Delete Files Matching Pattern

See more FTP Examples

The DeleteMatching method deletes all files in the current remote directory matching a wildcarded filename.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Ftp2;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$ftp = new Ftp2();

$ftp->Hostname = 'www.chilkatsoft.com';
$ftp->Username = 'MyLogin';
$ftp->Password = 'MyPassword';

// Connect and login to the FTP server.
try {
    $ftp->Connect();

    // Change to the remote directory where the files to be deleted are located.
    // for the FTP account.
    $ftp->ChangeRemoteDir('junk');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// Delete all files with filenames matching "ftp_*.asp"
$numDeleted = $ftp->DeleteMatching('ftp_*.asp');
if ($numDeleted < 0) {
    echo $ftp->LastErrorText, "\n";
    exit;
}

$ftp->Disconnect();

echo $numDeleted, " Files Deleted!\n";