Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a file on an ftp site using php cURL

Tags:

php

curl

ftp

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
like image 324
Parris Varney Avatar asked Oct 28 '25 19:10

Parris Varney


1 Answers

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.

like image 159
Parris Varney Avatar answered Oct 30 '25 09:10

Parris Varney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!