I have a file type and the button named "upload Image", where I select the file and upload the image. on clicking the upload image button image should be saved in the uploads folder.
On running in the localhost it works fine but fails to upload in the server.
This is my code:
$(document).ready(function() {
$('#upload').on('click', function() {
var file_data = $('#fileToUpload').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: "http://mpp.ABCDEF.co.in/API/sendingInterface.php?fn= uploadImage", // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(data) {
$('#response').val(data);
},
error: function() {
alert('Error');
}
});
});
});
Function of sendingInterface.php:
if($_GET['fn'] =="uploadImage")
{
$target_dir = $obj1->images_folder();
$target_file = $target_dir . basename($_FILES['file']['name']);
header ('Content-type: application/json');
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
http_response_code(200);
$encoded = json_encode("{ success }");
exit($encoded);
}
else {
http_response_code(200);
$encoded = json_encode("{ failure }");
exit($encoded);
}
}
Where $obj1->images_folder() is having the folder name in database as ../../MPP/images/uploads/
On running in server I am getting the output as a alert box with message "Error". How can I run it in server?
Try to manually set the full path to the upload folder - for an example: $target_dir="http://mpp.ABCDEF.co.in/API/MPP/images/uploads/" But you can't be sure with this relative path where the folder is. You have to find it by ftp-client or file manager - I don't know what is your hosting tools. At all it is not good idea to use relative paths, because probably the config path and your base points are different.