ftp_get() - ftp函数
ftp_get()
(PHP 4, PHP 5, PHP 7)
从 FTP 服务器上下载一个文件
说明
ftp_get(resource $ftp_stream,string $local_file,string $remote_file,int $mode[,int $resumepos= 0]): boolftp_get()函数用来下载 FTP 服务器上指定的文件并保存为本地文件。
参数
$ftp_streamFTP 连接的链接标识符。
$local_file文件本地的路径(如果文件已经存在,则会被覆盖)。
$remote_file文件的远程路径。
$mode传送模式。只能为(文本模式)FTP_ASCII
或(二进制模式)FTP_BINARY
中的其中一个。
从远程文件的这个位置继续下载。
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
范例
ftp_get()例子
更新日志
版本 | 说明 |
---|---|
4.3.0 | 增加了$resumepos。 |
参见
ftp_pasv()
返回当前 FTP 被动模式是否打开ftp_fget()
从 FTP 服务器上下载一个文件并保存到本地一个已经打开的文件中ftp_nb_get()
从 FTP 服务器上获取文件并写入本地文件(non-blocking)ftp_nb_fget()
从 FTP 服务器获取文件并写入到一个打开的文件(非阻塞)
I tried to ftp a 7mb file today off my webserver. I copied this example directly and it told me. Port command successful "there was a problem" I thought it was because of the size. But I guessed it might be cause of my firewall. So I made the ftp connection passive: Ran the script again & it worked fine. Hope this helps someone
Don't want to use an intermediate file? Use 'php://output' as the filename and then capture the output using output buffering. ob_start(); $result = ftp_get($ftp, "php://output", $file, FTP_BINARY); $data = ob_get_contents(); ob_end_clean(); Don't forget to check $result to make sure there wasn't an error. After that, manipulate the $data variable however you want.
Why there isn't an "ftp_get_contents" function, I don't know. It takes a little work to emulate one, but it's doable. Something similar would work to write a ftp_put_contents function, too.
Keep in mind that ftp_get will overwrite the file on your local machine if it has the same name.
ftp_sync is a way to walk the directory structure on the server and copy every directory and file to the same location locally.
Here's a quick function that figures out the correct mode to use based on a file's extension.
Crud. The _nb_ only refers to reading from the ftp server, and the buffer in the socket pair is only about 364 bytes. So it doesn't work for files larger than that size.
If you ran the example and see that it fail after 90sec (timeout). Then try adding:
Note that PHP still defaults to FTP active mode by default, which is almost never used anymore since the creation of firewalls. Don't forget to add a ftp_pasv($conn, true) after your ftp_login.
Hello everybody, If someone will try to download files to the same local file (some temporary file), like shown here: 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 file’s size is not equal to source file’s 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: Good luck in scripting :-) Vitali Simsive
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.
I am using ftp_get function to download a file from the FTP server to my web server where my php script is running. On the webserver, I want the file to be downloaded to a directory which has a structure as follows -> Data/Files/localfilename.exe However, when I specify the above string in the $local_file_name parameter of ftp_get I get an error saying the file does not exist. I am running a Windows Server and the php script is running from C:/xampp/htdocs/file.php Is there any way where I can specify the path on the web server where the file is supposed to be downloaded ?
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.
I'd suggest use ftp_fget() instead of ftp_get() since the latter only return TRUE or FALSE and there's no obvious way to get the cause of failure. Using ftp_fget, you have to pass a file handle as local file, so you have to do fopen() first. By way of this, you can find 'Permission Denied' problem when call fopen(). If you use ftp_get(), there's no way to find this error cause. In my case, I run httpd using 'nobody' and I create ftp local folder using 'haha'. It tooks me long time to find the 'Permission Denied' problem at that time since I use ftp_get() then.
On Windows (and possibly *NIX) you will get "[function.ftp-get]: failed to open stream: No such file or directory in..." errors if the local_file path contains directory paths that do not already exist. Even with write permissions ftp_get can create the file but it will NOT automatically create the parent directories as you might expect.
A subtle issue with the ftp_get() function. The second param, string $local_file, is a file name on the SERVER running the php script. It is NOT a file on the client machine running the browser. I erroneously tried to use this ftp to download a file from my site to my local system. I entered the full path starting with the drive letter ("h:/...") on a system running WIN XP and kept getting a failure of unable to open (destination) file. Only after just putting in a file name with no pathing did I see where the file was written. It was in the directory on my site where the php script is located (hosting is managed shared LAMP server which supports multiple url's, GoDaddy hosting).
Remember to use the full server paths to the directories you are working on. Server paths are not the same as "ftp paths". I was using the path displayed on my FTP client to download and upload files and I kept getting "Not found" or "Permission Denied" errors.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)