Uploading an image with PHP curl












0















How would I upload an image and the following input text from the form to with the help php curl? How do I post both an image file and text attributes using formData? Is there anything else I should use? Is there a simpler way to achieve this? I want to post both the image and the text fields. I have been trying many different methods, but none have seemed to work for me. If anyone can explain it to me as well that would be greatly appreciated.



Here is my form -->



<form id="form" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="name" name="name" type="text" class="form-control">
</div>
<div class="form-group">
<input id="file" name="file" type="file" class="form-control-file">
</div>

<button id="button" class="btn btn-lg btn-primary">
<span>Send</span>
</button>
</form>


This is my javascript -->



var form = $('form')[0]; 
formData = new FormData(form);
$.ajax({
url: 'apiController',
data: formData,
type: 'POST',
contentType: false,
processData: false,
});


This is my apiController -->



$imageFile = $_POST['file'];

function sendData() {
$url = "Here I will have my server"
$curl = curl_init();

$contentType = 'multipart/form-data';

curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $imageFile,
CURLOPT_HTTPHEADER => array(
$contentType;
),
));
curl_close($curl);
}









share|improve this question























  • This question has been asked before stackoverflow.com/a/21927891/156811

    – Havenard
    Nov 25 '18 at 21:08













  • Possible duplicate of What is the right way to POST multipart/form-data using curl?

    – billynoah
    Nov 25 '18 at 21:08











  • Also just to point out a few problems, this is not how you set headers in cURL, $_POST is not where you will find the file data, also just giving the file name to postfields is not going to get anything done, you are not telling cURL what you want.

    – Havenard
    Nov 25 '18 at 21:10











  • I have already tried this solution @Havenard. I was not able to get it working. Any other suggestions?

    – Nick55
    Nov 25 '18 at 21:12











  • Evidently you haven't, you're still doing everything wrong. Use the code from that answer I linked.

    – Havenard
    Nov 25 '18 at 21:13


















0















How would I upload an image and the following input text from the form to with the help php curl? How do I post both an image file and text attributes using formData? Is there anything else I should use? Is there a simpler way to achieve this? I want to post both the image and the text fields. I have been trying many different methods, but none have seemed to work for me. If anyone can explain it to me as well that would be greatly appreciated.



Here is my form -->



<form id="form" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="name" name="name" type="text" class="form-control">
</div>
<div class="form-group">
<input id="file" name="file" type="file" class="form-control-file">
</div>

<button id="button" class="btn btn-lg btn-primary">
<span>Send</span>
</button>
</form>


This is my javascript -->



var form = $('form')[0]; 
formData = new FormData(form);
$.ajax({
url: 'apiController',
data: formData,
type: 'POST',
contentType: false,
processData: false,
});


This is my apiController -->



$imageFile = $_POST['file'];

function sendData() {
$url = "Here I will have my server"
$curl = curl_init();

$contentType = 'multipart/form-data';

curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $imageFile,
CURLOPT_HTTPHEADER => array(
$contentType;
),
));
curl_close($curl);
}









share|improve this question























  • This question has been asked before stackoverflow.com/a/21927891/156811

    – Havenard
    Nov 25 '18 at 21:08













  • Possible duplicate of What is the right way to POST multipart/form-data using curl?

    – billynoah
    Nov 25 '18 at 21:08











  • Also just to point out a few problems, this is not how you set headers in cURL, $_POST is not where you will find the file data, also just giving the file name to postfields is not going to get anything done, you are not telling cURL what you want.

    – Havenard
    Nov 25 '18 at 21:10











  • I have already tried this solution @Havenard. I was not able to get it working. Any other suggestions?

    – Nick55
    Nov 25 '18 at 21:12











  • Evidently you haven't, you're still doing everything wrong. Use the code from that answer I linked.

    – Havenard
    Nov 25 '18 at 21:13
















0












0








0








How would I upload an image and the following input text from the form to with the help php curl? How do I post both an image file and text attributes using formData? Is there anything else I should use? Is there a simpler way to achieve this? I want to post both the image and the text fields. I have been trying many different methods, but none have seemed to work for me. If anyone can explain it to me as well that would be greatly appreciated.



Here is my form -->



<form id="form" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="name" name="name" type="text" class="form-control">
</div>
<div class="form-group">
<input id="file" name="file" type="file" class="form-control-file">
</div>

<button id="button" class="btn btn-lg btn-primary">
<span>Send</span>
</button>
</form>


This is my javascript -->



var form = $('form')[0]; 
formData = new FormData(form);
$.ajax({
url: 'apiController',
data: formData,
type: 'POST',
contentType: false,
processData: false,
});


This is my apiController -->



$imageFile = $_POST['file'];

function sendData() {
$url = "Here I will have my server"
$curl = curl_init();

$contentType = 'multipart/form-data';

curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $imageFile,
CURLOPT_HTTPHEADER => array(
$contentType;
),
));
curl_close($curl);
}









share|improve this question














How would I upload an image and the following input text from the form to with the help php curl? How do I post both an image file and text attributes using formData? Is there anything else I should use? Is there a simpler way to achieve this? I want to post both the image and the text fields. I have been trying many different methods, but none have seemed to work for me. If anyone can explain it to me as well that would be greatly appreciated.



Here is my form -->



<form id="form" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="name" name="name" type="text" class="form-control">
</div>
<div class="form-group">
<input id="file" name="file" type="file" class="form-control-file">
</div>

<button id="button" class="btn btn-lg btn-primary">
<span>Send</span>
</button>
</form>


This is my javascript -->



var form = $('form')[0]; 
formData = new FormData(form);
$.ajax({
url: 'apiController',
data: formData,
type: 'POST',
contentType: false,
processData: false,
});


This is my apiController -->



$imageFile = $_POST['file'];

function sendData() {
$url = "Here I will have my server"
$curl = curl_init();

$contentType = 'multipart/form-data';

curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $imageFile,
CURLOPT_HTTPHEADER => array(
$contentType;
),
));
curl_close($curl);
}






javascript php json ajax curl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 21:00









Nick55Nick55

614




614













  • This question has been asked before stackoverflow.com/a/21927891/156811

    – Havenard
    Nov 25 '18 at 21:08













  • Possible duplicate of What is the right way to POST multipart/form-data using curl?

    – billynoah
    Nov 25 '18 at 21:08











  • Also just to point out a few problems, this is not how you set headers in cURL, $_POST is not where you will find the file data, also just giving the file name to postfields is not going to get anything done, you are not telling cURL what you want.

    – Havenard
    Nov 25 '18 at 21:10











  • I have already tried this solution @Havenard. I was not able to get it working. Any other suggestions?

    – Nick55
    Nov 25 '18 at 21:12











  • Evidently you haven't, you're still doing everything wrong. Use the code from that answer I linked.

    – Havenard
    Nov 25 '18 at 21:13





















  • This question has been asked before stackoverflow.com/a/21927891/156811

    – Havenard
    Nov 25 '18 at 21:08













  • Possible duplicate of What is the right way to POST multipart/form-data using curl?

    – billynoah
    Nov 25 '18 at 21:08











  • Also just to point out a few problems, this is not how you set headers in cURL, $_POST is not where you will find the file data, also just giving the file name to postfields is not going to get anything done, you are not telling cURL what you want.

    – Havenard
    Nov 25 '18 at 21:10











  • I have already tried this solution @Havenard. I was not able to get it working. Any other suggestions?

    – Nick55
    Nov 25 '18 at 21:12











  • Evidently you haven't, you're still doing everything wrong. Use the code from that answer I linked.

    – Havenard
    Nov 25 '18 at 21:13



















This question has been asked before stackoverflow.com/a/21927891/156811

– Havenard
Nov 25 '18 at 21:08







This question has been asked before stackoverflow.com/a/21927891/156811

– Havenard
Nov 25 '18 at 21:08















Possible duplicate of What is the right way to POST multipart/form-data using curl?

– billynoah
Nov 25 '18 at 21:08





Possible duplicate of What is the right way to POST multipart/form-data using curl?

– billynoah
Nov 25 '18 at 21:08













Also just to point out a few problems, this is not how you set headers in cURL, $_POST is not where you will find the file data, also just giving the file name to postfields is not going to get anything done, you are not telling cURL what you want.

– Havenard
Nov 25 '18 at 21:10





Also just to point out a few problems, this is not how you set headers in cURL, $_POST is not where you will find the file data, also just giving the file name to postfields is not going to get anything done, you are not telling cURL what you want.

– Havenard
Nov 25 '18 at 21:10













I have already tried this solution @Havenard. I was not able to get it working. Any other suggestions?

– Nick55
Nov 25 '18 at 21:12





I have already tried this solution @Havenard. I was not able to get it working. Any other suggestions?

– Nick55
Nov 25 '18 at 21:12













Evidently you haven't, you're still doing everything wrong. Use the code from that answer I linked.

– Havenard
Nov 25 '18 at 21:13







Evidently you haven't, you're still doing everything wrong. Use the code from that answer I linked.

– Havenard
Nov 25 '18 at 21:13














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471932%2fuploading-an-image-with-php-curl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471932%2fuploading-an-image-with-php-curl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python