Delete a file via FTP (without PHP FTP support)
| From: | Charles Wiltgen | Date: | Thu, 07 Nov 2002 06:14:04 +0000 |
| Subject: | Delete a file via FTP (without PHP FTP support) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-123110@lists.php.net to get a copy of this message | ||
// Delete a file via FTP
function deleteFileViaFTP($ftpServer, $user, $password, $ftpDir, $ftpFile) {
$error = FALSE;
// Open connection to FTP port
$ftp = fsockopen($ftpServer,21);
$foo = fgets($ftp);
if (!ereg("^220*", $foo)) { $error = "*** Error! FTP service not ready
***\r\n"; }
// Submit user name and password
fputs($ftp,"USER $user\r\n");
$foo = fgets($ftp);
if (!ereg("^331*", $foo)) { $error = "*** Error! USER commend
unsuccessful ***\r\n"; }
fputs($ftp,"PASS $password\r\n");
$foo = fgets($ftp);
if (!ereg("^230*", $foo)) { $error = "*** Error! PASS command
unsuccessful ***\r\n"; }
// Change to the proper directory
fputs($ftp,"CWD $ftpDir\r\n");
$foo = fgets($ftp);
if (!ereg("^250*", $foo)) { $error = "*** Error! CWD command
unsuccessful ***\r\n"; }
// Delete the file
fputs($ftp,"DELE $ftpFile\r\n");
$foo = fgets($ftp);
if (!ereg("^250*", $foo)) { $error = "*** Error! DELEte command
unsuccessful\r\n"; }
// Wrap it up
fputs($ftp,"QUIT\r\n");
$foo = fgets($ftp);
if (!ereg("^221*", $foo)) { $error = "*** Error! FTP service closed
abnormally *** \r\n"; }
fclose($ftp);
// Return TRUE if the file has been deleted
$fileUri = "ftp://$user:$password@$ftpServer/$ftpDir/$ftpFile";
$fp = @fopen($fileUri, "r");
if (!$fp) { return TRUE; } else { return FALSE; }
}