Php uses curl to download ftp files,

Source: Internet
Author: User
Tags ftp file ftp client

Php uses curl to download ftp files,

The example in this article shows how to download ftp files in php for your reference. The details are as follows:

I don't know why the normal ftp_get function is very slow to download files, but the ftp client is very fast to download, so the curl download method is changed, and the speed is normal.

Function file_upload ($ ftpIp, $ ftpUser, $ ftpPwd, $ path, $ fileSavePath) {$ curlobj = curl_init (); // initialize // input the target ftp file, for example, 'ftp: // 192.168.3.1/test/1.jpg 'curl_setopt ($ curlobj, CURLOPT_URL, "ftp ://". $ ftpIp. "/". $ path); curl_setopt ($ curlobj, CURLOPT_HEADER, 0); // No output header curl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, 0 ); // time out after 300 s curl_setopt ($ curlobj, CURLOPT_TIMEOUT, 2000); // timeout // you can use this function to set the ftp user name and password, which is not required ! Curl_setopt ($ curlobj, CURLOPT_USERPWD, $ ftpUser. ':'. $ ftpPwd); $ outfile = fopen ($ fileSavePath, 'W + '); // name of the file saved to the local file curl_setopt ($ curlobj, CURLOPT_FILE, $ outfile ); $ rtn = curl_exec ($ curlobj); if (curl_errno ($ curlobj) {writeLog ('curl error :'. curl_error ($ curlobj);} fclose ($ outfile); curl_close ($ curlobj); if ($ rtn = 1) {return true ;} else {unlink ($ fileSavePath); // If the download fails but the file is opened locally, delete return false ;}}

It is normal to test the downloading of small files, but if the network speed is slow, an ftp timeout error will be reported when downloading large files. At present, I still don't know where to configure the problem or what the problem is.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.