簡體   English   中英

為什么上傳圖片時取消鏈接功能不會刪除舊圖片

[英]Why unlink function will not delete old image when uploading image

每次更新圖像時,我都必須手動進入文件夾。 該圖像將更新為新圖像。 但是,當我更新圖像時,不會刪除舊圖像。 我正在使用PHP函數取消鏈接來刪除圖像,但由於某種原因其無法正常工作。 我從代碼中的php取消鏈接中刪除了“ @”符號。 我已經重新編輯了代碼。 我不斷收到以下錯誤:

警告:取消鏈接(圖片/):第24行的C:\\ xampp \\ htdocs \\ upload_update \\ New_project \\ edit_image.php中的權限被拒絕

我正在嘗試自學php,非常感謝您的幫助。

這是代碼:

<?php
include "connection.php";
$vid="";
$vname="";
$vprice="";
$vpicture="";



if(isset($_POST["button_edit"])){
     $product_name = $_POST["product_name"];
     $product_price = $_POST["product_price"];
     $product_id = $_POST["product_id"];
     $old_picture = $_POST['old_picture'];
     if(!empty($_FILES["product_picture"]["name"])) {
    $product_picture = $_FILES["product_picture"]["name"];
    $qry = mysqli_query($con,"Update table_product Set product_name='$product_name', product_price='$product_price', product_picture='$product_picture' Where product_id='$product_id'");

        $target_dir = "picture/";
        $target_file = $target_dir . basename($_FILES["product_picture"]["name"]);
       $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    move_uploaded_file($_FILES["product_picture"]["tmp_name"],$target_file);
    if (isset($old_picture) && ($old_picture != $product_picture)) {
              unlink("picture/" . $old_picture);
            }
    }
    else{
 $qry = "Update table_product Set product_name='$product_name', product_price='$product_price' Where product_id='$product_id'";
}

$qryUpdate = mysqli_query($con,$qry);
    }

else if(isset($_GET["edit"])){
    $qry = mysqli_query($con,"Select * From table_product Where product_id='".$_GET["edit"]."'");
    while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
        $vid=$row["product_id"];
        $vname=$row["product_name"];
        $vprice=$row["product_price"];
        $vpicture=$row["product_picture"];
    }
}
?>

 <!DOCTYPE html>
<html>
<head>
<title>Product</title>
</head>
<body>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method="post" enctype="multipart/form-data" >
    <table>
        <tr>
            <td>Product ID</td>
            <td><input type="text" name="product_id" value="<?php echo $vid;?>"></td></tr>
        <tr><td>Product Name</td>
        <td><input type="text" name="product_name"  value="<?php echo $vname;?>"></td></tr>
        <tr><td>Product Price</td>
        <td><input type="text" name="product_price"  value="<?php echo $vprice;?>"></td></tr>
        <input type="hidden" name="old_picture" value="<?php if (!empty($old_picture)) echo $old_picture; ?>" />
        <tr><td>Product Picture</td>
        <td><input type="file" name="product_picture" ></td></tr>
        <?php if (!empty($old_picture)) {
        echo '<img class="profile" src="picture/' . $old_picture . '" alt="image" style=width:150px;height:xpx;">';
      } ?>
        <tr><td colspan="2">
        <input type="submit" name="button_add" value="Add">
        <input type="submit" name="button_edit" value="Edit"></td></tr> </table>
</form>
<table border=1>
    <tr><th>product ID</th><th>product Name</th>
    <th>product price</th><th>product image</th>  <th>Action</th></tr>
    <?php
    $qry =mysqli_query($con, "Select * From table_product");
    while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
        echo '<tr><td>'.$row["product_id"].'</td>';
        echo '<td>'.$row["product_name"].'</td>';
        echo '<td>'.$row["product_price"].'</td>';
        echo '<td><img src="picture/'.$row["product_picture"].'" style=width:100px;height:xpx;"/></td>';

        echo '<td><a href="?edit='.$row["product_id"].'&picture='.$row["product_picture"].'">Edit</a> </td></tr>';



    }



    ?>
</table>
<br><br><br>
</body>
</html>

從此行刪除@符號:

@unlink("picture/".$_GET["picture"])

這可能會向您顯示警告。 使用絕對路徑而不是相對路徑來取消鏈接,因此unlink應如下所示

@unlink(__DIR__."/picture/".$_GET["picture"]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM