Parris Varney
Parris Varney

Reputation: 11478

Moving a file on an ftp site using php cURL

I have a need to rename a file after download using php cURL.

Here's what I have:

    $localFile = fopen($fileName, 'w');

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FILE, $localFile);
    curl_setopt($curl, CURLOPT_USERPWD, "$ftpUsername:$ftpPassword");
    curl_setopt($curl, CURLOPT_POSTQUOTE, array("RNFR $remoteFile", "RNTO $remoteFile.downloaded"));

    curl_exec($curl) === false) {
        throw new Exception(curl_error($curl));
    }

If I remove the CURLOPT_POSTQUOTE part, the file downloads fine. I've also tried a couple different combinations of the postquote array:

    curl_setopt($curl, CURLOPT_POSTQUOTE, array("RNFR $remoteFile RNTO $remoteFile.downloaded"));
    curl_setopt($curl, CURLOPT_POSTQUOTE, array("-RNFR $remoteFile", "-RNTO $remoteFile.downloaded"));
    curl_setopt($curl, CURLOPT_POSTQUOTE, array("-RNFR $remoteFile -RNTO $remoteFile.downloaded"));
    curl_setopt($curl, CURLOPT_POSTQUOTE, array("rename $remoteFile $remoteFile.downloaded"));

The error I've been getting look like:

ERROR  : QUOT string not accepted: -RNFR $remoteFile -RNTO $remoteFile.downloaded

Upvotes: 4

Views: 5636

Answers (1)

Parris Varney
Parris Varney

Reputation: 11478

In case anybody finds themselves here in the future.

My issue was that $remoteFile resolved to something like /remote_folder/remote_file.txt, but since my ftp connection already included the folder, something like ftp://www.example.com/remote_folder/remote_file.txt, I wasn't supposed to specify the folder in RNFR and RNTO.

Upvotes: 4

Related Questions