4

I've been using a piece of nifty PHP code to upload files to FTP via cURL. It has served me well until today.

It returns a curl error #3 when I execute it Intepretation of error: CURLE_URL_MALFORMAT (3) : The URL was not properly formatted. I figured it was because the password contains special characters. The password contains a '<' e.g. R3lHK2A9<1 This code works in the past where the passwords all consist of only letters and numbers.

I tried using escapeshellarg() , urlencode() and escapeshellcmd() to the password....in vain. Am I missing something?

Can you guys help please?

    <?php
    $ch = curl_init();
    $localfile = “test.tar”;
    $fp = fopen($localfile, ‘r’);

    curl_setopt($ch, CURLOPT_URL, ‘ftp://username:password@ftp.domain.com/public_html/filesfromscript/’.$localfile);
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));

    curl_exec ($ch);
    $error_no = curl_errno($ch);
    curl_close ($ch);

    if ($error_no == 0) {
    $message = ‘File uploaded successfully.’;
    } else {
    $message = “File upload error: $error_no. Error codes explained here http://curl.haxx.se/libcurl/c/libcurl-errors.html”;
    }
    echo $message;
    ?>
dplante
  • 2,445
  • 3
  • 21
  • 27
Roy
  • 269
  • 2
  • 6
  • 12

2 Answers2

8

Try using the CURLOPT_USERPWD option to set the auth credentials, instead of passing then in the URL.

E.g.

curl_setopt($ch, CURLOPT_URL, 'ftp://ftp.domain.com/public_html/filesfromscript/'.$localfile);
curl_setopt($ch, CURLOPT_USERPWD, 'username:pass<word');

Also (taken from here) you could try percent-encoding the auth credentials:

$username = 'username';
$password = 'pass<word';
curl_setopt($ch, CURLOPT_URL, 'ftp://'.rawurlencode($username).':'.rawurlencode($password).'@ftp.domain.com/public_html/filesfromscript/'.$localfile);
Community
  • 1
  • 1
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
1

You can try to set login/pass using CURLOPT_USERPWD option :

curl_setopt(CURLOPT_USERPWD, '[username]:[password]') 
soju
  • 25,111
  • 3
  • 68
  • 70