【问题标题】:Why unlink function will not delete old image when uploading image为什么上传图片时取消链接功能不会删除旧图片
【发布时间】:2016-11-27 23:43:55
【问题描述】:

每次更新图像时,我都必须手动进入我的文件夹。图像将更新为新图像。但是,当我更新图像时,不会删除旧图像。我正在使用 PHP 函数 unlink 删除图像,但由于某种原因它不起作用。我已经从代码中的 php unlink 中删除了 at "@" 符号。我已经重新编辑了代码。我不断收到以下错误:

警告:unlink(picture/): Permission denied in C:\xampp\htdocs\upload_update\New_project\edit_image.php on line 24

我正在尝试自学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>

【问题讨论】:

  • 如果你不抑制错误,也许你会发现原因
  • 很可能是权限问题。查看文件夹和文件权限
  • “未定义索引:图片”表示在您的 $_GET 请求中,没有名为图片的变量。由于这是在您检查 $_POST 请求的代码块中,因此我猜测该请求未设置 GET 变量。简短回答:检查您的 URL 中是否有问号 ?

标签: php mysqli


【解决方案1】:

从该行中删除 @ 符号:

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

这可能会向您显示警告。 使用要取消链接的文件的绝对路径,而不是相对路径,因此您的 unlink 应该是这样的

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

【讨论】:

  • 我已经从代码中的 php unlink 中删除了 at "@" 符号。这些是我不断收到的 2 个错误:注意:未定义索引:第 22 行 C:\xampp\htdocs\upload_update\New_project\edit_image.php 中的图片警告:unlink(picture/): Permission denied in C:\xampp\htdocs \upload_update\New_project\edit_image.php 在第 22 行。感谢您的宝贵时间。
  • 检查你的 $_GET 数组是否包含 'picture' 索引,原因,警告告诉你 $_GET 中不存在这个索引
猜你喜欢
  • 2021-08-22
  • 2018-08-06
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 2021-01-22
  • 1970-01-01
  • 2021-05-02
  • 2010-11-21
相关资源
最近更新 更多