使用CURL通过SFTP删除文件

时间:2019-06-25 08:21:56

标签: php curl sftp delete-file

我想使用curl删除文件

我正在使用SFTP,下面是我正在使用的代码。不适用于我

    $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 个答案:

答案 0 :(得分:2)

DELE不是有效的SFTP命令,我认为您正在寻找命令rm,它应该类似于:

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

下次您遇到卷曲问题时,请启用CURLOPT_VERBOSE,然后执行

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

并同时发布VERBOSE日志和curl_errno()的输出以及curl_error()的输出