取消链接功能不起作用

时间:2013-03-29 03:53:03

标签: php

<?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

?>

一切正常,除了unlink功能我无法弄清楚我的旧文件变量有什么问题,它不是从文件夹中删除现有文件。

此外,如果我的更新中发生任何验证错误,我还需要知道为什么它会从url中丢失?edit=$id

1 个答案:

答案 0 :(得分: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");
}

(但有更好的错误处理)

相关问题