I have written a code for uploading an Image using PHP, it works fine on my localhost server but the function move_uploaded_file() is returning false when I have uploaded the website on an Online Server.
Here is my HTML code
<input type="file" name="selectImg" id="selectImg" accept="image/jpg, image/jpeg" style="display: none;">
<label for="selectImg" class="btn btn-primary" style="border-radius: 29px; outline: none;" > Chose a Image</label>
<button class="btn btn-success" style="border-radius: 29px;outline: none;" id="upload">Upload</button>
AJAX function for sending the data
function sendData(name,pid,file){
let data = new FormData();
data.append('file',file);
data.append('pid',pid);
data.append('name',name);
console.log(data);
$.ajax({
type: "post",
url: "upload.php",
data :data,
processData: false,
contentType: false,
success: function(data) {
// if(data=='UPLOADED SUCCESSFULLY'){
// alert(data);
// location.reload();
// }
alert(data);
}
});
}
Here is my JavaScript Code
uploadBtn.addEventListener("click",function(){
if(file){
let fName = file['name'].split(".");
// console.log(fName);
if(fName[1] == "jpeg" || fName[1] == "jpg"){
let pid = document.getElementById('pID').value;
let name = file['name'];
let url = window.location.href.split("=");
sendData(name,pid,file);
}else{
alert('Please Select a jpeg/jpg file');
}
}else{
alert('Please Select an image');
}
});
Here is the PHP CODE
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
$imgFile = $_FILES['file'];
$iName = $_POST['name'];
$loc = $_FILES['file']['tmp_name'];
$path = "../assets/uploads/".basename($_FILES['file']['name']);
$serverPath = "assets/uploads/".$iName;
$size = filesize($loc);
if(move_uploaded_file($loc,$path)){
$pathquery = "INSERT INTO `images` (`path`, `pid`, `status`) VALUES ('$serverPath', '$pid', 'ACTIVE');";
echo $pathquery;
$sendPathQuery = mysqli_query($conn,$pathquery);
}else{
echo "File Not Uploaded";
}
}else{
echo "FILE NOT UPLOADED !!!";
}
The above code works perfectly on the localhost server but when uploaded the move_uploaded_file() function is returning false.