Using FTP from PHP CURL
I can't figure out how to use CURL's FTP, specifically, 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开发者_JS百科,CURLOPT_POSTQUOTE,array('CWD bussys/','LIST')); /* ?!! */
echo '<hr><pre>'.htmlspecialchars(curl_exec($ch)).'</pre><hr>';
?>
In my example above I want to get a directory listing of bussys, but instead I get a listing of the main (FTP root) directory.
By the way, I tried the following combinations:
LIST bussys/
CWD bussys, LIST -a
I searched for a way to change working directory also, but couldn't find anything.
What ultimately worked for me was including the folder in the CURLOPT_URL
. Additionally, the folder alone wasn't enough, I had to include a trailing forward slash /
.
So if what worked for me will work for you, then setting the following url structure and CURLOPT_FTPLISTONLY
should get you the contents of bussys/
.
curl_setopt($ch, CURLOPT_URL, 'ftp://ftp.microsoft.com/bussys/');
curl_setopt($ch, CURLOPT_FTPLISTONLY, TRUE);
And I didn't need the CURLOPT_POSTQUOTE
CWD
option to get the nested directories contents.
If you want to work with curl, use CURLOPT_CUSTOMREQUEST options.
See below example code.
$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;
It returns
type=dir;modify=20130319024302; test
test is sub directory of a.
I think you have to use ftp_connect rather.
精彩评论