سؤال

I am making an edit form where the user can edit item details and change the item image. The item details get updated in the database fine but the item image does now change. Below is my code...

if($_POST['item_img']){
    $old_img = "img/items/$item_id.jpg";
    $default_img = "img/items/default.jpg";
    if (file_exists($old_img)) {
        unlink($old_img);
        $filename = $item_id.'.jpg';
        move_uploaded_file ($_FILES["item_img"]["tmp_name"],"img/items/".$filename)
    }elseif(file_exists($default_img)) {
        unlink($default_img);
        $filename = $item_id.'.jpg';
        move_uploaded_file ($_FILES["item_img"]["tmp_name"], "img/items/".$filename);
    }
}

I think it is my if($_POST['item_img']) statement that is causing this issue. What could I change the loop to.

The first nested if statement checks if there is an image attached, and the second if statement checks if a default image is attached. In both cases it deletes the old image and sets a new image with the "item_id" as its name.

Below is the code for my for upload field:

<form class="stock" method="post" enctype="multipart/form-data" action="">'
     <input type="file" name="item_img">
</form>
هل كانت مفيدة؟

المحلول

Change it

$_POST['item_img']

To

$_FILES["item_img"]["tmp_name"]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top