Problem Downloading .GZ via FTP

Discussion in 'PHP' started by FPForum, Jun 25, 2014.

  1. #1
    Hey everyone..I'm trying to download a file onto my server using FTP and PHP. The file I'm trying to download is a .GZ file and for some reason I keep getting the following error

    first, my code
    
    <?php
    $conn_id = ftp_connect("hostname.com");
    $login_result = ftp_login($conn_id, "username", "password");
    
    if ((!$conn_id) || (!$login_result)) {
    echo "FTP connection has failed!";
    exit;
    } else {
    echo "Connected";
    }
    
    // get the file
    $local = fopen("file.gz","w");
    $result = ftp_fget($conn_id, $local,"home/username/public_html/folder/file.gz", FTP_BINARY);
    
    // check upload status
    if (!$result) {
    echo "FTP download has failed!";
    } else {
    echo "Downloaded ";
    }
    
    // close the FTP stream
    ftp_close($conn_id);
    ?>
    
    Code (markup):
    next, the error
    
    PHP Warning:  ftp_fget() [<a href='function.ftp-fget'>function.ftp-fget</a>]: Failed to open file. in /home/username/public_html/folder/file.php on line 14
    
    Code (markup):
    Any idea what could be causing this? I see the file get created locally like it's trying to download - but it's always 0 bytes in size. I know some of you will say to just use the FTP link instead - but the server is locked to my IP so it must be downloaded locally..

    Any help is appriciated!
     
    FPForum, Jun 25, 2014 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    There probably should be a forward slash in front of "home" in your path.

    Other than that, what are the folder permissions of "folder"? Is PHP allowed to write to this folder?
     
    nico_swd, Jun 25, 2014 IP
  3. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #3
    Thanks for your reply @nico_swd ...Are you sure there needs to be forward slash in front of home? I didn't think that was required.

    Great point on the permissions - those are currently at 0755 so I think the server should be able to write to the folder just fine. Besides, the file does seam to get created there so apparently it can write just fine - but the file is 0 in size.
     
    FPForum, Jun 25, 2014 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Two things (#2 is the major one imo):

    1) If you don't have the slash, then the file being worked on is relative to the working directory. Adding the / to home makes the path absolute instead of relative.

    2) As stated on the docs: http://www.php.net/manual/en/function.ftp-get.php, $local should be a location of a file, not the actual file handler.
     
    ThePHPMaster, Jun 25, 2014 IP
  5. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #5
    Thanks for your response @ThePHPMaster ...One question, if I replace the $local variable then I don't need the following?
    
    $local = fopen("file.gz","w");
    
    Code (markup):
    Thanks!
     
    FPForum, Jun 25, 2014 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    You still need the local variable, just not as a resource:

    
    $local = 'file.gz';
    
    PHP:
     
    ThePHPMaster, Jun 25, 2014 IP
  7. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #7
    Awesome, I'll try this. thanks!!
     
    FPForum, Jun 25, 2014 IP
  8. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #8
    Now I appear to be getting a new error:
    [26-Jun-2014 09:00:04] PHP Warning: ftp_fget() expects parameter 2 to be resource, string given in /home/username/public_html/folder/file.php on line 15

    Where line 15 is
    
    $result = ftp_fget($conn_id, $local,"/home/username/public_html/folder/file.gz", FTP_BINARY);
    
    Code (markup):
    Here is my new code in full
    
    <?php
    $conn_id = ftp_connect("hostname.com");
    $login_result = ftp_login($conn_id, "username", "password");
    
    if ((!$conn_id) || (!$login_result)) {
    echo "FTP connection has failed!";
    exit;
    } else {
    echo "Connected";
    }
    
    // get the file
    $local = "file.gz";
    $result = ftp_fget($conn_id, $local,"/home/username/public_html/folder/file.gz", FTP_BINARY);
    
    // check upload status
    if (!$result) {
    echo "FTP download has failed!";
    } else {
    echo "Downloaded ";
    }
    
    // close the FTP stream
    ftp_close($conn_id);
    ?>
    
    Code (markup):
     
    FPForum, Jun 25, 2014 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    There's a difference between ftp_get() and ftp_fget(). They're almost the same, except that ftp_fget() takes a resource as parameter, whereas ftp_get() takes a path.

    Try ftp_size() with the remote path, and see if it returns the actual file size you're expecting. If it returns -1, the file does likely not exist.
     
    nico_swd, Jun 26, 2014 IP
    ThePHPMaster and FPForum like this.