您的位置:首页 > 编程语言 > PHP开发

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

2016-03-20 20:03 746 查看
CURL

<?php
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);
?>


<?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;
}
}
?>


FTP

<?php
$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."<br>";
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);curl_close($ch);
fclose($fp);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: