客户端的文件上传到服务器,服务器返回文件的路径 返回信息,客户端将文件保存
客户端:
<?php
header(‘content-type:text/html;charset=utf8‘);
$url = ‘http://192.168.1.118/legcc/aaa.php‘;//访问的服务器的地址
$curl = curl_init();
$path = ‘D:\www\ceshi\a02.jpeg‘;//客户端文件的绝对路径
$source = file_get_contents($path);
$dir = date(‘Y/m/d/‘,time());
$data = array(‘source‘=>$source,‘name‘ => ‘a02‘,‘ext‘=>‘.jpeg‘,‘dir‘=>$dir);//参数
$re = curl_setopt($curl, CURLOPT_URL, $url);
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);
if(json_decode($result)->errrorMessage ==1) {
var_dump( json_decode($result));
} else if(json_decode($result)->errrorMessage ==2){
echo ‘文件已经上传过~‘;
} else {
echo ‘上传失败~!‘;
}
服务端:
<?php
//服务端收到客户端传过来的文件保存
$basePath = ‘E:\www\legcc/‘; //文件存放的目录
$path = $basePath . $_POST[‘dir‘];
$results=[];
if (!is_dir($path)) {
mkdir($path,0777,true);
}
if (!is_file($path .$_POST[‘name‘].$_POST[‘ext‘])) {
$fuck = file_put_contents($path .$_POST[‘name‘].$_POST[‘ext‘], $_POST[‘source‘]);
if ($fuck) {
$path1 = ‘http://192.168.1.118/legcc/‘.$_POST[‘dir‘]. rand(10,99).$_POST[‘ext‘];
$results = [
errorCode=>$path1,
errrorMessage=>1
];
}else{
$results = [
errrorMessage=>0
];
}
}else{
$results =[
errrorMessage=>2
];
}
echo json_encode($results);