У меня есть HTML-форма для загрузки изображения (изображение qr), чтобы отправить его с помощью метода 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>
Я использовал метод post ajax, чтобы отправить это изображение в файл process.phpдля CURL
Мой пост 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";
}
}
Я уверен, что файловый сеанс не отправляется в процессФайл .php, когда я пытался использовать var_dump ($ filename), он показывает значение NULL. Есть ли правильный способ добиться этого. В основном я хочу выбрать файл из формы начальной загрузки и отправить его в конечную точку API поверх файла process.php.
Спасибо за любые предложения