0
<?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此外,如果我的更新中发生任何验证错误,我还需要知道为什么它会丢失来自 url。

4

1 回答 1

0

首先检查以确保文件存在,然后测试 unlink 调用的结果。

本质上:

$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");
}

(但有更好的错误处理)

于 2013-03-29T04:06:20.607 回答