程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php curl模仿ftp文件上傳代碼

php curl模仿ftp文件上傳代碼

編輯:關於PHP編程

php教程 curl模仿ftp文件上傳代碼
<body>
<form action="curlupload.php" method="post" enctype="multipart/form-data">
<div>
<label for="upload">select file</label>
<input name="upload" type="file" />
<input type="submit" name="submit" value="upload" />
</div>
</form>
</body>
</html>
<?
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:[email protected]/'.$_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.';
 }
}
?>

好了下面封閉成類了

<?php
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 = '未選擇文件';
         }
  }
    }
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved