如何使用类从目录中删除文件

时间:2014-10-18 13:49:55

标签: php

我在从数据库中删除数据时尝试从文件夹中删除文件。我用了一堂课。 我收到以下错误:

  

调用未定义的函数getCodeName()

//This function is to get the file name from the database
public static function getCodeName($product_id)   {
    $db = Database::getDB();
    $query = "SELECT * FROM products WHERE productID = '$product_id'";
    $result = $db ->query($query);
    $row = $result->fetch();
    $product = new Product ($row['productCode']);

    return $product;
}

//Delete file from folder and other data from database
public static function deleteProduct($product_id) {

    $fileName = getCodeName($product_id);
    $dir = "../images/";
    unlink($dir.DIRECTORY_SEPARATOR.$fileName);
    $db = Database::getDB();
    $query = "DELETE FROM products
              WHERE productID = '$product_id'";
    $row_count = $db->exec($query);
    return $row_count;
}

1 个答案:

答案 0 :(得分:1)

使用php代码

unlink()功能设置完全删除文件