I have a problem trying to upload a file from a form. The form with the file input is created using ajax.
ajax-getForm.php
<input id="uploadCouponPhoto" name="uploadCouponPhoto" type="file">
Then I use javascript on a button
<input id="popup_couponBox_commandButton" type="button" onclick="javascript: coupons_apply();" value="Appliquer">
to execute another ajax file that saves the form.
ajax-saveCoupon.php
$customPhoto = @$_POST['uploadCouponPhoto'];
if(!empty($customPhoto)){
$name = "coupon_".$idCoupon;
$directory = $_SERVER["DOCUMENT_ROOT"]."/resources/images/members/".$idMember;
if(move_uploaded_file($customPhoto, $directory."/".$name)){
echo "success";
}
else{
var_dump($_FILES['uploadedfile']['error']);
}
}
However, the response of ajax-saveCoupon.php is always false. I've tried to see the errors using $_FILES['uploadedfile']['error']
but this gives NULL.var_dump($_FILES)
gives an empty array. I can also see uploadCouponPhoto: C:\fakepath\small-logo2.jpeg
in the form data of ajax-saveCoupon headers. I'm wondering what I'm not doing correctly for this to work, is it because the move_uploaded_file is not in the same file as the input?
Thanks.
You can't simply POST
files with AJAX. You can use the FileReader API to read the file and build your own multipart/form-data
request.
This is quite involved and there are many tutorials on how to do it. Good luck.