Befee1975 Befee1975 - 3 months ago 12
PHP Question

Update input type="file" and keep path for image

This is my code

<?php
include "../../../config/config.php";
session_start();

if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
die("Not found");
}

if (isset($_POST['submit-edit'])) {

$title = mysqli_real_escape_string($con, $_POST['title']);
$description = mysqli_real_escape_string($con, $_POST['description']);
$category = mysqli_real_escape_string($con, $_POST['category']);



/* ----------------------- MAIN IMAGE -------------------------- */
$target_dir = "../../../img/find/thumbs-categorii/";
$target_file2 = "" . basename($_FILES["img-edit"]["name"]);
$target_file = $target_dir . basename($_FILES["img-edit"]["name"]);

$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

// Check file size
if ($_FILES["img-edit"]["size"] > 100000) {
$_SESSION['image-size'] = 1;
exit();
}
//
//Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != NULL) {
$_SESSION['image-format'] = 1;

exit();
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {

// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["img-edit"]["tmp_name"], $target_file)) {



} else {
echo "Sorry, there was an error uploading your file.";
// exit();
}


$query = "UPDATE descopera_second" .
"SET title='" . $title . "', text='" . $description . "', image='" .
$target_file2 . "', fk_descopera_first='" . $category .
"' WHERE id=" . $id;

var_dump($query);
exit();
$result = mysqli_query($con, $query);
// var_dump($query);
// exit();
if ($result) {

$_SESSION['edit_slider'] = 1;
header("Location: /dashboard/");
} else {
//

header("Location: /dashboard/");
}
}
}
?>


I want to keep the current path in database if the input with the image is empty. I don't know why, but my code currently stops at
echo "Sorry, there was an error uploading your file.";
and changes the path for the image, in my database. If the input is empty I want to keep the current path, because I just want to edit.

Answer

You define $uploadOk = 1, but that value never changes. So the following code, will always execute the else part of your condition.

if ($uploadOk == 0) {

} else {

    /* Everything in here will be executed */

}

Now look at all of the code that inside of that one conditional block.

The first thing is another condition.

if (move_uploaded_file($_FILES["img-edit"]["tmp_name"], $target_file)) {

} else {
    echo "Sorry, there was an error uploading your file.";
    // exit();
}

Where you say your code is stopping means that move_uploaded_file failed. This would be expected if the user did not supply a file upload in the request. Because $target_file will be empty, and an empty string is probably not going to be a valid path on your filesystem, plus there's the fact that there was no file upload in the first place even if it were.

Everything else that happens inside that first conditional block then continues to happen anyway, unconditionally (i.e. updating your database even though there was no file uploaded).

$query = "UPDATE descopera_second" .
         "SET title='" . $title . "', text='" . $description . "', image='" .
         $target_file2 . "', fk_descopera_first='" . $category .
         "' WHERE id=" . $id;

Try using var_dump to inspect the value of $target_file2 when this happens. It won't be what you expected. We know this because you initialize it as $target_file2 = "" . basename($_FILES["img-edit"]["name"]); above, and there was no file upload. So $_FILES is empty.

var_dump($target_file2);

So the better way to do this is to check if a valid file upload occurs first before attempting to update this value in your database. You probably meant to do this with your $uploadOk variable at the top.

if (isset($_FILES["img-edit"]["tmp_name"])) {
    $query = "UPDATE descopera_second" .
             "SET title='" . $title . "', text='" . $description . "', image='" .
             $target_file2 . "', fk_descopera_first='" . $category .
             "' WHERE id=" . $id;
} else {
    $query = "UPDATE descopera_second" .
             "SET title='" . $title . "', text='" . $description . "'," .
             ", fk_descopera_first='" . $category .
             "' WHERE id=" . $id;
}