php把文件上传到远程服务器上例子

<?php
    header('content-type:text/html;charset=utf8');

    $file = dirname(__FILE__).'/1.jpg';
    $data['img'] = new CurlFile($file);
    $url = "http://localhost/test/img/get_img.php";
    
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $result = curl_exec($ch);
    curl_close($ch);
    
    echo json_decode($result);
?>
远程服务器接受端代码:
<?php 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; } } ?>

 

posted @ 2018-10-08 15:08  泥土里的绽放  阅读(1847)  评论(0编辑  收藏  举报