簡體   English   中英

取消鏈接功能不起作用

[英]unlink function not working

<?php

if (isset($_GET['editpic'])) {
    $Id         = $_GET['editpic'];
    //Get Form Data from Data base against the Id
    $Edit_Query = "SELECT product_picture FROM products WHERE id='$Id'";
    $Result     = mysql_query($Edit_Query);
    while ($Row = mysql_fetch_array($Result)) {
        $Old_File_Name = $Row['product_picture'];
    }
}

//To Update Record
if (isset($_POST['update'])) {
    $Id                 = $_POST['id'];
    //To get file
    $Allowed_Extensions = array(
        'jpg',
        'jpeg',
        'png',
        'bmp',
        'gif'
    );
    $Allowed_Size       = 2097152;
    $File_Name          = $_FILES['newfile']['name'];
    $File_Size          = $_FILES['newfile']['size'];
    $File_Tmp           = $_FILES['newfile']['tmp_name'];
    $File_Explode       = (explode('.', $File_Name));
    $File_Extension     = strtolower(end($File_Explode));
    //Form Validation
    $Errormessage       = array();
    if (empty($File_Name)) {
        $Errormessage[] = "Please choose an image for your product";
    }
    if (!in_array($File_Extension, $Allowed_Extensions)) {
        $Errormessage[] = "Please choose only image file";
    }
    if ($File_Size > $Allowed_Size) {
        $Errormessage[] = "Maximum file limit is 2Mb";
    }
    if (empty($Errormessage)) {
        unlink("product_images/" . $Old_File_Name);
        if (move_uploaded_file($File_Tmp, "product_images/" . $File_Name)) {
            //To Rename the uploaded file
            $Random        = rand() * 1200;
            $File_New_Name = $Random . "." . $File_Extension;
            rename("product_images/" . $File_Name, "product_images/" . $File_New_Name);
            $Query  = "UPDATE products SET product_picture='$File_New_Name'    WHERE id='$Id'";
            $Result = mysql_query($Query);
            if ($Result) {
                header("location: manage_inventory.php");
            }

        }


    }

} //End isset update

?>

除了取消鏈接功能之外,其他所有東西都在工作,我無法弄清楚舊文件變量出了什么問題,它不是從文件夾中刪除現有文件。

另外,如果我的更新中發生任何驗證錯誤,我還需要知道為什么它會從網址中?edit=$id

首先檢查以確保文件存在,然后測試取消鏈接調用的結果。

實質上:

$fileToDelete = "product_images/".$Old_File_Name;
if(!file_exists($fileToDelete)) {
   die("Eh? I can't delete something that doesn't exist!");
}

if(!unlink($fileToDelete)) {
   die("I can't delete that file, check my permissions");
}

(但具有更好的錯誤處理能力)

暫無
暫無

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

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