zoukankan      html  css  js  c++  java
  • 客户端的文件上传到服务器,服务器返回文件的路径

    客户端的文件上传到服务器,服务器返回文件的路径 返回信息,客户端将文件保存

    客户端:

    <?php
    header('content-type:text/html;charset=utf8');
    $url = 'http://192.168.1.118/legcc/aaa.php';//访问的服务器的地址
    $curl = curl_init();
    $path = 'D:wwwceshia02.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:wwwlegcc/'; //文件存放的目录
    $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);

  • 相关阅读:
    [ZJOI2008]树的统计 树链剖分
    CF915E 动态开线段树
    Poj 2114 Boatherds(点分治)
    Poj 2599 Godfather(树的重心)
    Bzoj 2152: 聪聪可可(点分治)
    Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)
    Cogs 329. K- 联赛(最大流)
    Cogs 731. [网络流24题] 最长递增子序列(最大流)
    Bzoj 2282: [Sdoi2011]消防(二分答案)
    Cogs 732. [网络流24题] 试题库(二分图)
  • 原文地址:https://www.cnblogs.com/l-zl/p/6972626.html
Copyright © 2011-2022 走看看