Bootsrap输入文件格式和CURL返回NULL

时间:2019-10-01 17:35:50

标签: ajax curl bootstrap-4

我有一个用于图像上传(qr图像)的html表单,以使用CURL方法(我的表单脚本)发送它:

<form  id="imagef" action="" method="post" enctype="multipart/form-data" data-persist="garlic" data-destroy="true" data-domain="true" >
    <div class="pd-20 col-sm-12 col-md-12" style="padding: 20px 0; width: 90%;">
        <div class="custom-file">
          <input type="file" class="custom-file-input" id="imagef" name="imagef">
          <label class="custom-file-label" for="digest">Choose file</label>
        </div>
        </br>

        <button id="verify" name="verify" class="btn btn-outline-primary data-style="expand-right" data-spinner-color="#337ab7">Send</button>

    </div>

</form>

我使用ajax post方法将该图像发送到CURL的process.php文件中

我的ajax帖子:

$(document).ready(function(){
    $('#imagef').submit(function(){


        $.ajax({
            type: 'POST',
            url: 'services/process.php', 
            data: $(this).serialize()
        })
        .done(function(data){

            // show the response
            $('#response').html(data);

        })
        .fail(function() {

            // just in case posting your form failed
            alert( "Posting failed." );

        });

        // to prevent refreshing the whole page page
        return false;

    });
});

process.php

if (isset($_POST['uploadedFile']))
{
$url = "http://endopint/api/image"; 
$filename = $_FILES['uploadedFile']['name'];
$filedata = $_FILES['uploadedFile']['tmp_name'];
$filesize = $_FILES['uploadedFile']['size'];
if ($filedata != '')
{
    $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
    $postfields = array("image" => "@$filedata", "image" => $filename);
    $ch = curl_init();
    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => true,
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_INFILESIZE => $filesize,
        CURLOPT_RETURNTRANSFER => true
    ); // cURL options
    curl_setopt_array($ch, $options);
    curl_exec($ch);
    if(!curl_errno($ch))
    {
        $info = curl_getinfo($ch);
        if ($info['http_code'] == 200)
            $errmsg = "File uploaded successfully";
    }
    else
    {
        $errmsg = curl_error($ch);
    }
    curl_close($ch);
}
else
{
    $errmsg = "Please select the file";
}
}

我确定文件会话没有发送到process.php文件,当我尝试使用var_dump($ filename)时,它显示NULL值。是否有实现此目标的正确方法。基本上我想做的是,从引导表单中选择文件,然后通过process.php文件提交给api端点。

感谢您的任何建议

0 个答案:

没有答案