Delete file over SFTP using CURL

Viewed 2037

I want to delete the file using curl

I am using SFTP and below is the code that I am using. It is not working for me

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_URL, "sftp://use:@101.101.101.101/");
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
    curl_setopt($ch, CURLOPT_PORT, 51402);
    curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, '/home/web/.ssh/id_rsa');
    curl_setopt($ch, CURLOPT_SSH_PUBLIC_KEYFILE, '/home/web/.ssh/id_rsa.pub');
    curl_setopt($ch, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PUBLICKEY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_QUOTE, array("DELE /dispatch/confirm/W00027478.csv"));
    $result = curl_exec($ch);
    $error_no = curl_errno($ch);
    curl_close($ch);

    if ($error_no == 0) {
        echo "Delete File $filename<br>";
    } else {
        echo "File Not Delete $filename<br>";
    }
1 Answers

DELE is not a valid SFTP command, i think you're looking for the command rm, it should look something like:

curl_setopt($ch, CURLOPT_QUOTE, array("rm ".escapeshellarg("/dispatch/confirm/W00027478.csv"));

and next time you have issues with curl, enable CURLOPT_VERBOSE, and do

var_dump(curl_errno($ch),curl_error($ch));

and post both the VERBOSE log and the output of curl_errno() and the output of curl_error()

Related