How to use curl to implement ftp file download function in php

墨辰丷
Release: 2023-03-27 13:16:01
Original
2052 people have browsed it

This article mainly introduces in detail how PHP uses curl to implement the ftp file download function. It has certain reference value. Interested friends can refer to it

The specific content is as follows

I don’t know why using the normal ftp_get function to download files is very slow, but using the ftp client downloads quickly, so I changed the curl download method and the speed is normal

function file_upload($ftpIp,$ftpUser,$ftpPwd,$path,$fileSavePath){
  $curlobj = curl_init();//初始化
  //传入ftp的目标文件,如'ftp://192.168.3.1/test/1.jpg'
  curl_setopt($curlobj,CURLOPT_URL,"ftp://".$ftpIp."/".$path);
  curl_setopt($curlobj,CURLOPT_HEADER,0);//不输出header
  curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,0);
  //time out after 300s 
  curl_setopt($curlobj,CURLOPT_TIMEOUT,2000);//超时时间
  //通过这个函数设置ftp的用户名和密码,没设置就不需要! 
  curl_setopt($curlobj,CURLOPT_USERPWD,$ftpUser.':'.$ftpPwd);

  $outfile = fopen($fileSavePath,'w+'); //保存到本地文件的文件名 
  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);//如果下载失败,但是本地open了这个文件,所以要删除
    return false;
   }

}
Copy after login

It is normal for the test to download small files, but if the network speed is very slow and a large file is downloaded, an ftp timeout error will be reported. I still don’t know what the configuration problem is or what the problem is.

Related recommendations:

php sends XML data through curl and obtains XML data

php curlDetailed explanation of the steps to obtain the return value

php implements curlupload, download, https login

The above is the detailed content of How to use curl to implement ftp file download function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template