문제

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