Home  >  Article  >  Backend Development  >  php curl imitates ftp file upload code_PHP tutorial

php curl imitates ftp file upload code_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:09:051094browse

php教程 curl模仿ftp文件上传代码










if (isset($_post['submit'])) {
 if (!empty($_files['upload']['name'])) {
     $ch = curl_init();
     $localfile = $_files['upload']['tmp_name'];
     $fp = fopen($localfile, 'r');
     curl_setopt($ch, curlopt_url, 'ftp://ftp_login:password@ftp.domain.com/'.$_files['upload']['name']);
     curl_setopt($ch, curlopt_upload, 1);
     curl_setopt($ch, curlopt_infile, $fp);
     curl_setopt($ch, curlopt_infilesize, filesize($localfile));
     curl_exec ($ch);
     $error_no = curl_errno($ch);
     curl_close ($ch);
        if ($error_no == 0) {
            $error = 'file uploaded succesfully.';
        } else {
            $error = 'file upload error.';
        }
 } else {
        $error = 'please select a file.';
 }
}
?>

好了下面封闭成类了

class curl_ftp
{
   
    private $ftpname;          //ftp用户名
    private $ftppaw;           //ftp密码
    private $urlftp;           //ftp地址
    private $filename;         //文件名
   
    public __construct($name, $password, $ftp)
    {
        $this->ftpname  = $name;
        $this->ftppaw   = $password;
        $this->urlftp   = $ftp;
 //    $this->filename = $filename;
    }
   
    public function getftp()
    {
        if (isset($_post['submit']))
      {
         if (!empty($_files['upload']['name']))
         {
             $ch = curl_init();
             $this->filename = $_files['upload']['tmp_name'];
             $fp = fopen($this->filename, 'r');
             curl_setopt($ch, curlopt_url, $this->ftp.$this->filename);
            curl_setopt($ch, curlopt_userpwd, "$name:password");
             curl_setopt($ch, curlopt_upload, 1);
             curl_setopt($ch, curlopt_infile, $fp);
             curl_setopt($ch, curlopt_infilesize, filesize($this->filename));
             curl_exec ($ch);
             $error_no = curl_errno($ch);
             curl_close ($ch);
                if ($error_no == 0)
                {
                    $error = '文件上传成功';
                }
                else
                {
                    $error = '文件上传失败';
                }
         }
         else
         {
                $error = '未选择文件';
         }
  }
    }
}


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444823.htmlTechArticlephp教程 curl模仿ftp文件上传代码 body form action=curlupload.php method=post enctype=multipart/form-data div label for=uploadselect file/label input name=upload type=file...
Statement:
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