通过php中间件上传文件不起作用

时间:2018-07-24 09:46:17

标签: php ajax curl file-upload

我创建了一个golang文件上传功能,当我与邮递员进行测试时可以正常工作。但是我需要通过php中间件调用该函数,但我对php并不熟悉,请帮助解决此问题。以下是我的ajax调用和php代码。

var createUpload = function (data) {
    return $.ajax({
        url: "https://"+window.location.hostname+"xxx/index.php?action=uploadCSV&filename="+onlyName[0],
        type: 'POST',
        cache: false,
        contentType: false,
        processData: false,
        data: data,
        success: function (returndata) {
            alert(returndata);
        }
    });
};

case 'uploadCSV':
        if ($method == 'POST') {
            //$uploadCSV = file_get_contents('php://input');

            $profileImg = $_FILES['file'];

            foreach ($profileImg as $result) {
                echo $result, '<br>';
            }

            $uploadCSVReturn = $vamps->uploadCSVFile($profileImg, $filename);
            echo $uploadCSVReturn;
        } else {
            echo "POST Required";
        }
        break;


public function uploadCSVFile($user,$filename){
        $service_url = $this->vampsBaseURL . '/xxx/'.$filename;
        $curl = curl_init($service_url);
        $data_string = json_encode($user);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $user);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: multipart/form-data',
            'Authorization: Bearer ' . $this->authToken
        ));
        $curl_response = curl_exec($curl);
        if ($curl_response === false) {
            $info = curl_getinfo($curl);
            curl_close($curl);
            die('error occurred during curl exec. Additional info: ' . var_export($info));
        }
        // curl_close($curl);
        return $curl_response;
    }

0 个答案:

没有答案