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!
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?
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.
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.
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!
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):
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.