简体   繁体   中英

cURL PHP FTP on hosting via limited list opened ports

My hosting can access to outside servers via limited list opened ports. I connect to outside server via right port, but not get file list etc. because port for Extended Passive Mode closed:

*   Trying 62.183.ХХ.ХХ:5666...
* TCP_NODELAY set
* Connected to 62.183.ХХ.ХХ (62.183.ХХ.ХХ) port 5666 (#0)
< 220 Microsoft FTP Service
> USER ****
< 331 Password required
> PASS ****
< 230 User logged in.
> PWD
< 257 "/" is current directory.
* Entry path is '/'
> EPSV
* Connect data stream passively
* ftp_perform ends with SECONDARY: 0
< 229 Entering Extended Passive Mode (|||50276|)
*   Trying 62.183.ХХ.ХХ:50276...
* TCP_NODELAY set
* Connecting to 62.183.ХХ.ХХ (62.183.ХХ.ХХ) port 50276
* Operation timed out after 5001 milliseconds with 0 bytes received
* Closing connection 0

What can do? PS

$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, FTPIP);
    curl_setopt($ch, CURLOPT_PORT, FTPPORT);
    curl_setopt($ch, CURLOPT_USERPWD, FTPUSER.":".FTPPASS);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_DIRLISTONLY, '1L');
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_STDERR, $verbose = fopen('php://temp', 'rw+'));
    $files_list = curl_exec($ch);
    $urlEndpoint = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    echo "Verbose information:\n<pre>", !rewind($verbose), htmlspecialchars(stream_get_contents($verbose)), "</pre>\n";
    curl_close($ch);

here's how i would solve it: first get a cheap vps (like this or this , $2/month for 1.5TB bandwidth, not bad,), then install nginx+libnginx-mod-http-lua on the vps, and make a script to mount your target ftp server on nginx's default docroot. like /mounter.sh containing

curlftpfs FTPIP /var/www/html/FTPIP -o ftp_method=singlecwd -o no_verify_hostname -o no_verify_peer -o utf8 -o allow_other -o big_writes -o user='FTPUSER:FTPPASS'

then add

@reboot /bin/sh /mounter.sh

to your root's crontab -e so it will auto-remount on reboots, then run the script, check that it mounts fine.. then in /etc/nginx/sites-enabled/default add

location /FTPIP_file_list {
  content_by_lua_block {
    os.execute("/bin/sh -c 'cd /var/www/html/FTPIP; find'");
  } 
}

and finally change your php script to

$ch = curl_init();
curl_setopt_array($ch,array(
    CURLOPT_URL => "http://vps_ip/FTPIP_file_list",
    CURLOPT_RETURNTRANSFER=>1
));
$files_list = curl_exec($ch);

... geez that was harder than expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM