将文件上传到我的rest api

时间:2018-04-17 18:07:16

标签: php html rest php-curl

我想用我的其他api上传文件到我的谷歌云存储与php curl这个PHP代码

if( $_SERVER['REQUEST_METHOD'] == 'POST' ){

        $file = $_FILES['files'];
        $fileName = $file['name'];
        $fileType = $file['type'];
        $fileTmp = $file['tmp_name'];
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "http://acquired-backup-198200.appspot.com/upload?uname=arun&Description=description&players=arun,sai&community=true&Location=US&skills=batting&analysis=true",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"Cielo_estrellado_by_Eduardo_Diez_Viñuela\"\r\nfile=\"filedir\"; filedir=\"@/home/alaa/Desktop/Cielo_estrellado_by_Eduardo_Diez_Viñuela.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
      CURLOPT_HTTPHEADER => array(
        "Cache-Control: no-cache",
        "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    };
    }

这是Html

<form  method="post" enctype="multipart/form-data">
        <input type="file" name="files">
        <input type="submit" name="">
    </form>

该过程成功,文件出现在存储中,但它们是0字节请帮帮我。

0 个答案:

没有答案