search
Homephp教程PHP源码php把文件上传到远程服务器上例子

在这里我们利用curl实现把本地服务器的文件通过curl发送请求给远程服务器的php文件接受就实现了上传,还一个是利用ftp来上传方法也是php中的curl操作ftp服务器进行上传。

<script>ec(2);</script>

我这里写的是用curl的代码

本地代码如下:

 代码如下 复制代码

    header('content-type:text/html;charset=utf8');
    $curl = curl_init();
    $data = array('img'=>'@'. dirname(__FILE__).'/img/login.gif');
    curl_setopt($curl, CURLOPT_URL, "http://www.demo.com/uploadimg.php");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($curl);
    curl_close($curl);
    echo json_decode($result);
?>

远程服务器接受端代码:

 代码如下 复制代码

    if($_FILES)
    {
        $filename = $_FILES['img']['name'];
        $tmpname = $_FILES['img']['tmp_name'];
        if(move_uploaded_file($tmpname, dirname(__FILE__).'/img/'.$filename))
        {
            echo json_encode('上传成功');
        }
        else
        {
            $data = json_encode($_FILES);
            echo $data;
        }
    }
?>

例子,同样是利用curl不过这是利用ftp

 代码如下 复制代码

$localfile = "php_homepage.txt";
$fp = fopen ($localfile, "r");
$arr_ip = gethostbyname(www.111cn.net);
echo $arr_ip;
$ftp = "ftp://".$arr_ip."/public_html/".$localfile; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, '***:****');
curl_setopt($ch, CURLOPT_URL, $ftp);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
$error = curl_error($ch);
echo $error."
";
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);curl_close($ch);
fclose($fp);
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools