PHP - unlink不会删除文件

时间:2014-12-05 03:00:46

标签: php file directory delete-file unlink

基本上,下面的代码工作正常,除了' unlink'部分。记录在数据库中找到并删除,但服务器目录中的文件保持不变。

我知道' pageLocation'是准确的,因为我已经用它来加载文件。

<?php
session_start();
if(isset($_POST['btnSubmitEdit'])) 
{
    $allowed_filetypes = array('.jpg','.jpeg','.png');
    $max_filesize = 10485760;
    $setComic = $_SESSION['comicID'];
    $setPage = $_SESSION['currentPage'];
    $upload_path = 'comics/'.$setComic.'/';

    $filename = $_FILES['fileToEdit']['name'];
    $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

    if(!in_array($ext,$allowed_filetypes))
    die('The file you attempted to upload is not allowed.');

    if(filesize($_FILES['fileToEdit']['tmp_name']) > $max_filesize)
    die('The file you attempted to upload is too large.');

    if(!is_writable($upload_path))
    die('You cannot upload to the specified directory, please CHMOD it to 777.');

    include_once('includes/conn.inc.php');

    $query1 = ("SELECT pageLocation FROM page WHERE pageID = '$setPage'");
    $result1 = mysqli_query($conn, $query1);
    while ($row = $result1->fetch_assoc())
    {
        unlink('/"'.$row['pageLocation'].'"');
    }

    if(move_uploaded_file($_FILES['fileToEdit']['tmp_name'],$upload_path . $filename)) 
    {
        mysqli_query($conn, "UPDATE page SET pageLocation='".$upload_path . $filename."' WHERE pageID=".$setPage.""); 
        mysqli_close($conn);
    }

    else 
    {
        echo 'There was an error during the file upload.  Please try again.';
    }
    header('Location: uploadPage.php');
}
if(isset($_POST['btnSubmitDelete']))
{
    $setPage = $_SESSION['currentPage'];
    include_once('includes/conn.inc.php');

    $query2 = ("SELECT pageLocation FROM page WHERE pageID = '$setPage'");
    $result2 = mysqli_query($conn, $query2);
    while ($row = $result2->fetch_assoc())
    {
        unlink('/"'.$row['pageLocation'].'"');
        mysqli_query($conn, "DELETE FROM page WHERE pageID = '$setPage'");
    }
    header('Location: uploadPage.php');
}
?>

0 个答案:

没有答案