PHP FTP connection problem

Discussion in 'PHP' started by stats, Sep 14, 2007.

  1. #1
    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
     
    stats, Sep 14, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Post the code you're using.
     
    nico_swd, Sep 14, 2007 IP
    stats likes this.
  3. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #3
    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
     
    stats, Sep 14, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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:
     
    nico_swd, Sep 14, 2007 IP
  5. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #5
    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
     
    stats, Sep 14, 2007 IP
  6. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #6
    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
     
    stats, Sep 14, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    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.
     
    nico_swd, Sep 14, 2007 IP
  8. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #8
    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
     
    stats, Sep 14, 2007 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    
    $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:
     
    nico_swd, Sep 14, 2007 IP
  10. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #10
    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 .. ?
     
    stats, Sep 14, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    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:
     
    nico_swd, Sep 14, 2007 IP
  12. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #12
    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
     
    stats, Sep 14, 2007 IP
  13. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #13
    Well you have FTP access to it. :D

    But if that isn't an option... then I'm running out of ideas too.
     
    nico_swd, Sep 14, 2007 IP
  14. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #14
    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
     
    stats, Sep 14, 2007 IP
  15. aRo`

    aRo` Peon

    Messages:
    141
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    i was looking for the same solution last week.

    you can find how to setup an ftp connection here
     
    aRo`, Sep 15, 2007 IP