Upload the file to FTP via PHP cURL, error, because the password contains the character "<"

I use a piece of excellent PHP code to upload files to FTP via cURL. It has served me well until today.

It returns a curl # 3 error when I execute it. Intepretation of error: CURLE_URL_MALFORMAT (3): The URL was not formatted correctly. I realized that this is because the password contains special characters. Password contains '<' for example R3lHK2A9 <1 This code works in the past when all passwords consist only of letters and numbers.

I tried using escapeshellarg() , urlencode() and escapeshellcmd() for the password .... in vain. Did I miss something?

Can you guys help?

  <?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; ?> 
+4
source share
2 answers

Try using the CURLOPT_USERPWD parameter to set authorization credentials instead of passing a URL.

eg.

 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 can try percent coding of auth credentials:

 $username = 'username'; $password = 'pass<word'; curl_setopt($ch, CURLOPT_URL, 'ftp://'.rawurlencode($username).':'.rawurlencode($password).'@ftp.domain.com/public_html/filesfromscript/'.$localfile); 
+6
source

You can try to set the username / password using the CURLOPT_USERPWD option:

 curl_setopt(CURLOPT_USERPWD, '[username]:[password]') 
+1
source

Source: https://habr.com/ru/post/1399986/


All Articles