2017-10-19 70 views
0

我想從我的服務器現有的文件複製到使用curlPHP捲曲這麼想的現有文件複製到外部服務器

Form class有兩個方法外部服務器。 insert_data方法創建該文件並將其插入到本地服務器中的現有文件夾和表中。

第二種方法試圖將文件

遠程CURLOPT_URL點複製到一個遠程服務器的現有文件夾到upload.php文件中的遠程服務器。

我有問題,

  1. 由於某種原因,我無法從insert_data()方法內部激活copy_file_to_ext_server()方法。

  2. 我無法將文件傳輸到遠程服務器。

這是本地服務器上的類:

<?php 
/** 
* Form class inserts form data to dataabse 
*/ 
class Form 
{ 

    function __construct() 
    { 

     $this->dbh = dbconnect::getDbh(); 
     if (!$this->dbh) { 
      die('Could not connect: ' . mysql_error()); 
     } 
    } 

    public function insert_data($lname, $fname, $idNo, $img, $upload_dir) 
    { 

     $stmt = $this->dbh->prepare('SELECT * FROM vitur_sodiut WHERE idno=?'); 
     $stmt->bindParam(1, $idNo); 
     $stmt->execute(); 
     $row = $stmt->fetch(PDO::FETCH_ASSOC); 

     if (!$row) { 
      $img  = str_replace('data:image/png;base64,', '', $img); 
      $img  = str_replace(' ', '+', $img); 
      $data  = base64_decode($img); 
      $file_name = $idNo . ".png"; 
      $file  = $upload_dir . "" . $idNo . ".png"; //file destination 
      $success = file_put_contents($file, $data); 



      $query = $this->dbh->prepare("INSERT INTO vitur_sodiut (lname, fname, file, idno) VALUES(?, ?, ?, ?)"); 
      $query->bindparam(1, $lname); 
      $query->bindparam(2, $fname); 
      $query->bindparam(3, $file); 
      $query->bindparam(4, $idNo); 

      self::copy_file_to_ext_server($file, $file_name); 


      try { 
       $query->execute(); 
       if (!$query) { 
        echo "Failure conncting db!"; 
       } 
      } 
      catch (PDOException $e) { 
       die($e->getMessage()); 
      } 
     } else { 
      echo 'value already exists in the table'; 
     } 
    } 


    public function copy_file_to_ext_server($file, $file_name) 
    { 
    echo $file; 

     $ch = curl_init(); 
     $cfile = new CURLFile($file, 'image/png', $file_name); 
     print_r($cfile); 
     $filedata = array(
      "myimage" => $cfile 
     ); 

     curl_setopt($ch, CURLOPT_URL, "http://myserver.co/folder/upload.php"); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $cfile); 

     $response = curl_exec($ch); 
     if ($response == true) { 
      echo "file posted"; 
     } else { 
      echo "Error" . curl_error($ch); 
     } 
    } 
} 
?> 

這是http://myserver.co/folder/upload.php內容:

<?php 

if(isset($file)){ 
    $path = "uploads/". $file_name; 
    move_uploaded_file($file, $path); 
} 

?> 

我也是用這個方法,而不是copy_file_to_ext_server,但沒有成功嘗試。

/* Remote File Name and Path */ 
$local_file = $file; 

/* FTP Account (Remote Server) */ 
$ftp_host = 'http://myserver'; /* host */ 
$ftp_user_name = 'username'; /* username */ 
$ftp_user_pass = 'password'; /* password */ 


/* File and path to send to remote FTP server */ 
$remote_file = '/public_html/a11y/' . $file_name; 

/* Connect using basic FTP */ 
$connect_it = ftp_connect($ftp_host); 


/* Login to FTP */ 
$login_result = ftp_login($connect_it, $ftp_user_name, $ftp_user_pass); 

/* Send $local_file to FTP */ 
if (ftp_put($connect_it, $remote_file, $local_file, FTP_BINARY)) { 
    echo "WOOT! Successfully transfer $local_file\n"; 
} 
else { 
    echo "Doh! There was a problem\n"; 
} 

/* Close the connection */ 
ftp_close($connect_it); 

我總是收到Doh! There was a problem錯誤thogh我把FTP連接如服務器的詳細信息。

+0

爲什麼是U試圖使用'curl'作爲中介,如果你要上傳過'FTP'後的文件? – DarkBee

+0

我不想通過ftp上傳它,只需將現有文件從服務器傳輸到服務器即可。 – DavSev

+0

底部的ftp代碼是將文件上傳到外部服務器的另一種方式,但它也不起作用。 – DavSev

回答

0

保持簡單。

$post = array($file,'image/png',$file_name); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "http://myserver.co/folder/upload.php"); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
相關問題