2014-11-06 126 views
0

我有兩個網站之一是A另一個是B我有一個圖像上傳表單A它允許用戶上傳files.A也有一個數據庫的圖像記錄路徑文件實際上已經去了B如何做到這一點?我擁有這兩個網站。任何想法?。如何使用另一個網站上傳一個網站上的文件

+1

可以爲此使用捲曲。 – Shubanker 2014-11-06 06:32:08

+0

你有關於這方面的文件嗎? – arunwebber 2014-11-06 06:33:47

回答

2

你可以簡單地使用PHP FTP 做到這一點見下文代碼 -

$connection = ftp_connect($server); 

$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass); 

    if (!$connection || !$login) { die('Connection attempt failed!'); } 

$upload = ftp_put($connection, $dest, $source, $mode); 

    if (!$upload) { echo 'FTP upload failed!'; } 

ftp_close($connection); 
+0

考慮我從表單上傳了一個文件,但是在源代碼中我必須從表單中獲得文件? – arunwebber 2014-11-07 14:05:25

+0

source將爲$ _FILES [「your_file_name_given_in_form」] [「tmp_name」]; – 2014-11-08 05:21:17

0

試試這個代碼:

if (version_compare(phpversion(), '5.5.0', '>=')) 
    { 
     $postData['file'] = new CURLFile($fileData['tmp_name'], $fileData['type'] ,$fileData['name']); 
    } 
    else 
    { 
     $postData['file'] = '@'.$fileData['tmp_name']; 
     $postData['file_info']['name'] = $fileData['name']; 
    } 

    $url = 'http://your-site.com/'; 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST,1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 

    $output= curl_exec($ch); 

    if ($output === false) 
    { 
     //handle the error 
    } 

    curl_close ($ch); 

注意:如果你有PHP 5.5及更高版本,建議使用CURLFile。

0

以下代碼可能會有所幫助。

<?php 
$target="http://youraddress.tld/example/upload.php"; 

# http://php.net/manual/en/curlfile.construct.php 

// Create a CURLFile object/procedural method 
$cfile = curl_file_create('resource/test.png','image/png','testpic'); // try adding 

// Create a CURLFile object/oop method 
#$cfile = new CURLFile('resource/test.png','image/png','testpic'); // uncomment and use if the upper procedural method is not working. 

// Assign POST data 
$imgdata = array('myimage' => $cfile); 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $target); 
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15'); 
curl_setopt($curl, CURLOPT_HTTPHEADER,array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://someaddress.tld','Content-Type: multipart/form-data')); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true); // enable posting 
curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); // post images 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload 
$r = curl_exec($curl); 
curl_close($curl); 

?> 

Source

+0

在這不需要認證從另一臺服務器? – arunwebber 2016-01-16 05:39:25