Guys, what would be the best way to connect to an FTP server using a php script ? what i'm trying to do is get the file (example) ftp://111.111.111.111/myfile.txt the server uses password i'm messing around with CURL and ftp_connect .. none of them brings luck to me yet .. it simply doesn't connect
currently i have this $ch = curl_init("ftp://111.111.111.111/myfile.txt"); curl_setopt($ch, CURLOPT_USERPWD, "[myloginhere]:[mypasshere]"); curl_exec($ch); curl_close($ch); PHP: I guess there's some mess going on with passive mode and some other FTP things which i barely have an idea of any suggestions ? Thanks
Try $ch = curl_init("ftp://username:password@111.111.111.111/myfile.txt"); curl_setopt($ch, CURLPT_RETURNTRANSFER, true); $file_contents = curl_exec($ch); curl_close($ch); PHP:
Thanks definetly an easier way to do the things, but still doesn't work i probably need to check if there is an IP limitation on the FTP server which prevents my server's ip to connect to that FTP location
Are there any options i may experiment with ? I know one thing only .. i am able to connect to that FTP using my internet explorer
What error do you get? If none, try setting these two lines on top of the script and see if it makes a difference. error_reporting(E_ALL); ini_set('display_errors', '1'); PHP: This method works for me on my host.
what i'm getting is just a blank page after a long pause (so i guess the connection just times out) however, when connecting to the same thing from my desktop - it connects immediately and my server is in good hosting too .. so that's not a network problem .. it's either they have IP filtering, or there's some option i have to set to connect to the FTP wich i don't know .. such as passive mode or something like that
$conn = ftp_connect('111.111.111.111'); ftp_login($conn, 'username', 'password'); if (ftp_get($conn, 'myfile.txt', 'path/to/myfile.txt', FTP_ASCII)) { echo 'File copied'; } else { echo 'An error occurred'; } ftp_close($conn); PHP: Hmm, try this. Try adding this if it doesn't work, to make the transfer in passive mode. ftp_pasv($conn, true); PHP:
ok, i guess i got it .. i've just contacted my hosting .. they told i need to purchase a dedicated IP for my site for the outgoing ftp connections to work .. otherwise they're being blocked .. so sad .. know any way to get around that ? tunneling, proxying, etc .. ?
You could create a transfer file on the target host. Something that would verify a password and a path, given by cURL's POST method. <?php if (isset($_POST['password'], $_POST['path'])) { if ($_POST['password'] == 'secretpass' AND is_file($_POST['path'])) { readfile($_POST['path']); exit(); } } ?> PHP:
Thanks i wish i had access to the remote host .. it's not my server .. anyway .. i guess my only option now is to purchase their dedicated IP nico - You're a very helpful guy i have added you a rep Thanks a lot
Thanks man, you're very supportive What i did is just ordered a dedicated IP from the hosting to be able to use this FTP things that's stupid .. it kept me 3-4 hours of trial and failure (and i belive 95% of the methods i've tried were valid) to realize this may be a stupid restriction from my hosting company .. anyway, i guess it should now be resolved .. just waiting for them to give me the new IP and retry the script .. i believe it should work