I have create a PHP file which is run using AJAX for uploading user's file to the server and update it's link to the database table. While running the file, I found that my PHP links file address with the database table successfully but the file is not moved in the destination folder that is 'uploads' folder. Unfortunately I'm unable to find the error. Could you help me in finding the error! MY PHP code is -
<?php
session_start();
//error_reporting(0);
$error = 0;
if($_FILES["image"]["name"] !='')
{
$lastid = $_SESSION['lastid'];
$img_name = $_FILES["image"]["name"];
$tempname = $_FILES["image"]["tmp_name"];
$img_size = $_FILES["image"]["size"];
$img_ext = explode('.',$img_name);
$ext_check = strtolower(end($img_ext));
$ext_allwd = array('png','jpg','jpeg');
$new_name = $lastid ."newname".".".$ext_check;
$area = "uploads/".$new_name;
if($ext_check !='jpg' || $ext_check!='jpeg' || $ext_check!='png'){
$error = 1;
echo "Only JPG, PNG or JPEG files are allowed.";
}
if($img_size > 512000 || $img_size < 51200){
echo "File size must be 50kb to 500kb";
$error = 1;
}
else{
$error = 0;
}
if($error==0){
// Create Connection
include 'connect.php';
$conn = mysqli_connect ($host, $dbusername, $dbpassword, $dbname);
$sql = "UPDATE `admndata` SET `imagename` = '$area' WHERE id = '$lastid'";
//link with Database
if (mysqli_query($conn, $sql)) {
//move uploaded file
move_uploaded_file($tempname,$area);
// close connection after finishing the job
mysqli_close($conn);
}
else {
echo "Error updating record: " . mysqli_error($conn);
// close connection after an error!
mysqli_close($conn);
}
}
}
?>
Edit : Code edited after accepting suggestions via comments.