PHP curl不会将现有文件复制到外部服务器

时间:2017-10-19 06:29:53

标签: php curl

我正在尝试使用Edit -> format -> Beautify Query

将现有文件从我的服务器复制到外部服务器

curl有两种方法。 Form class方法创建文件并将其插入本地服务器中的现有文件夹和表。

第二种方法尝试将文件复制到远程服务器到现有文件夹

远程insert_data指向远程服务器中的CURLOPT_URL文件。

我有问题,

  1. 出于某种原因,我无法从upload.php方法中激活copy_file_to_ext_server()方法。

  2. 我无法将文件传输到远程服务器。

  3. 这是本地服务器上的类:

    insert_data()

    这是<?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 ); 错误thogh我把ftp连接服务器的详细信息。

1 个答案:

答案 0 :(得分: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 );