Using FTP from PHP CURL

I cannot figure out how to use CURL FTP, in particular, how to issue FTP commands from my PHP code:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'ftp://ftp.microsoft.com/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch,CURLOPT_POSTQUOTE,array('CWD bussys/','LIST')); /* ?!! */

    echo '<hr><pre>'.htmlspecialchars(curl_exec($ch)).'</pre><hr>';
?>

In my example above, I want to get a list of bussys directories, but instead I get a list of the main (FTP root) directory.

By the way, I tried the following combinations:

  • LIST bussys/
  • CWD bussys, LIST -a
+3
source share
2 answers

I was looking for a way to change the working directory, but did not find anything.

What ultimately worked for me was to include the folder in CURLOPT_URL. Also, folders alone are not enough; I had to include backslashes /.

, , , , URL- CURLOPT_FTPLISTONLY bussys/.

curl_setopt($ch, CURLOPT_URL, 'ftp://ftp.microsoft.com/bussys/'); curl_setopt($ch, CURLOPT_FTPLISTONLY, TRUE);

CURLOPT_POSTQUOTE CWD, .

+2

curl, CURLOPT_CUSTOMREQUEST.

. .

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "ftp://192.168.0.129");
curl_setopt($curl, CURLOPT_USERPWD, "sru:sru");
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'CWD /a'); // change directory
curl_exec($curl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'MLSD'); // get directory list
$ftp_result=curl_exec($curl);
echo $ftp_result;

type=dir;modify=20130319024302; test

test - .

, ftp_connect .

-1

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


All Articles