如何使用另一个网站在一个网站上传文件

时间:2014-11-06 06:30:52

标签: php

我有两个网站,一个是A,另一个是B我在A中有一个图片上传表单,允许用户上传文件。还有一个数据库记录路径图像文件实际上是要去B如何做到这一点?我拥有这两个网站。有什么想法?。

3 个答案:

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

答案 1 :(得分: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。

答案 2 :(得分: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