Question

This code is behaving strangely. All I am trying to do is delete a file over an FTPS connection and determine if it worked. The delete is happening perfectly, but I am experiencing a side-effect. I can't figure out why, but something is causing the resulting directory contents to echo.

$conn = curl_init();
$testfile = 'file.txt';

curl_setopt($conn, CURLOPT_PORT, $config['imt']['ftp']['port']);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($conn, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);

$url = 'ftps://' . $config['imt']['ftp']['host'];

curl_setopt($conn, CURLOPT_URL, $url);
curl_setopt($conn, CURLOPT_USERPWD, $config['imt']['ftp']['username'] . ':' . $config['imt']['ftp']['password']);
curl_setopt($conn, CURLOPT_QUOTE, array('DELE /' . $testfile));

$result = curl_exec($conn);

if ($result)
{
    echo 'Success';
}

curl_close($conn);

The above prints out exactly this.

drwxrwxrwx   1 DCG AbilityFTPServer          0 Jan 09 12:58 .
drwxrwxrwx   1 DCG AbilityFTPServer          0 Jan 09 12:58 ..
-rw-rw-rw-   1 DCG AbilityFTPServer        260 Jan 08 12:52 Congratulations.txt
Success

The file I was trying to delete has, indeed, been removed, leaving only that txt file in the directory. It even prints "Success." But WHY is it printing my directory contents when I am not asking for it!?

Was it helpful?

Solution

Try curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); to cause the curl_exec() function to return the data instead of outputting it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top