I'm trying to add a category for images. Everything is fine working in my code. But the image is not moving to the image folder. I see the file name in the database column, so what is wrong in my php code?
I don't see any obvious syntax error:
<?php
require('top.inc.php');
$categories = '';
$msg = '';
if (isset($_GET['id']) && $_GET['id'] != '') {
$id = get_safe_value($link, $_GET['id']);
$res = mysqli_query($link, "select * from categories where id='$id'");
$check = mysqli_num_rows($res);
if ($check > 0) {
$row = mysqli_fetch_assoc($res);
$categories = $row['categories'];
} else {
header('location:categories.php');
die();
}
}
if (isset($_POST['submit'])) {
$filename = $_FILES["uploadfile"]["name"];
$tempname = $_FILES["uploadfile"]["tmp_name"];
$folder = "image/" . $filename;
$categories = get_safe_value($link, $_POST['categories']);
$res = mysqli_query($link, "select * from categories where categories='$categories'");
$check = mysqli_num_rows($res);
if ($check > 0) {
if (isset($_GET['id']) && $_GET['id'] != '') {
$getData = mysqli_fetch_assoc($res);
if ($id == $getData['id']) {
} else {
$msg = "Categories already exist";
}
} else {
$msg = "Categories already exist";
}
// Now let's move the uploaded image into the folder: image
if (move_uploaded_file($tempname, $folder)) {
$msg = "Image uploaded successfully";
} else {
$msg = "Failed to upload image";
}
}
if ($msg == '') {
if (isset($_GET['id']) && $_GET['id'] != '') {
mysqli_query($link, "update categories set categories='$categories' where id='$id'");
} else {
mysqli_query($link, "insert into categories(categories,filename,status) values('$categories','$filename','1')");
}
header('location:categories.php');
die();
}
}
?>