ftp_get

(PHP 3 >= 3.0.13, PHP 4, PHP 5)

ftp_get -- 从 FTP 服务器上下载一个文件

说明

bool ftp_get ( resource ftp_stream, string local_file, string remote_file, int mode [, int resumepos] )

ftp_get() 函数用来下载 FTP 服务器上由 remote_file 参数指定的文件,并保存到由参数 local_file 指定的本地文件。传送模式参数 mode 只能为 (文本模式) FTP_ASCII 或 (二进制模式) FTP_BINARY 中的其中一个。

注: 参数 resumepos 仅在适用于 PHP 4.3.0 以上版本.

如果成功则返回 TRUE,失败则返回 FALSE

例子 1. ftp_get() 例子

<?php
// define some variables
$local_file = 'local.zip';
$server_file = 'server.zip';
// connect to the FTP server
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo
"Successfully written to $local_file\n";
} else {
    echo
"There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>

参见 ftp_fget()ftp_nb_get()ftp_nb_fget()


add a note add a note User Contributed Notes
bugman at swfla dot rr dot com
14-Sep-2006 03:35
A number of people, myself included, have misinterpreted the term "local file" to mean that ftp_get will download a file to a client machine.  I think it's important to emphasize that "local" means where the PHP script resides, and not the node from which a user accesses that script.
administrator at gesoft dot org
13-Aug-2006 06:05
Hello everybody,

If someone will try to download files to the same local file (some temporary file), like shown here:

<?php
foreach ($files as $key=>$path) {
...
 
$result = ftp_get($ftpConnId, 'temp.tmp', $path, FTP_BINARY);
...
}
?>

please take in consideration the fact that you will have big problems with downloading (getting) hole files. In other words temp.tmp file always will have the same size equal to first downloaded file despite the real size of downloading file. I have not idea what is the reason!

If someone will think that problem is just in getting proper file size (which you will get using filssize() function) he will be mistaken. The download files size is not equal to source files size materially, that means fflush() function will not solve the problem (I have tried this as well).

Finally the solution was founded: before downloading a file you will need to delete local file if such exist (temp.tmp). So working code will look like:

<?php
foreach ($files as $key=>$path) {
...
  if (
file_exists('temp.tmp')) {
  
unlink('temp.tmp');
  }
 
$result = ftp_get($ftpConnId, 'temp.tmp', $path, FTP_BINARY);
...
}
?>

Good luck in scripting :-)

Vitali Simsive
corey-holzer at nyc dot rr dot com
23-Jan-2004 10:20
The zero size file is not a side effect.  When the ftp_get starts the first thing it does is to create the inode/file which it will stream the data too and that is a zero size file with the nname you specified for the local file.  When the download fails it leaves the file in place.
thivierr at telus dot net
23-Nov-2003 06:25
If you previously downloaded a file before (like a huge web log), and just want to get the remaining portion, do this:

$local_file_size = filesize($local_file_path);
$get_result = ftp_get($conn_id, $local_file_path, $remote_file_path, FTP_BINARY, $local_file_size);

This same code works regardless of wether the local file exists already or not.  You should first test to make sure the local file is not bigger than the remote file.
Logan
05-Sep-2003 08:21
If you want, you can save in any other directory in your hard disk, for example:

ftp_get($ftpstream, "C:\somefile.exe", "somefile.exe", FTP_BINARY);
phpnet at hostar dot com dot ar
10-Jun-2003 02:03
The file transferred will be saved in the folder where the ftp_get function is executed.
So, you will need the write permission in that folder.

In other way, you will obtain a "cant open local file" error.
ramiro at qusarcr dot com
06-Nov-2002 11:36
Keep in mind that ftp_get will overwrite the file on your local machine if it has the same name.