Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to FTP via PHP's cURL, error because password contains '<' character

Tags:

linux

php

curl

ftp

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:[email protected]/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;
    ?>
like image 492
Roy Avatar asked Jan 18 '23 07:01

Roy


2 Answers

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);
like image 74
DaveRandom Avatar answered Jan 20 '23 17:01

DaveRandom


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

curl_setopt(CURLOPT_USERPWD, '[username]:[password]') 
like image 32
soju Avatar answered Jan 20 '23 15:01

soju