0

我正在将图像上传到文件夹。我可以将图像上传到文件夹,但是当我想从文件夹中删除图像时,它不起作用。我不知道我做错了什么。要从文件夹中删除图像,我使用 unlink 方法。

这是我的代码:

<?php

$allowedExts = array("gif", "jpeg", "jpg", "png");
$images = "";

if(isset($_FILES['file'])){

    foreach($_FILES['file']['tmp_name'] as $key => $tmp_name)
    {
        $temp = explode(".", $_FILES['file']['name'][$key]);
        $file_name = $key.$_FILES['file']['name'][$key];
        $file_size = $_FILES['file']['size'][$key];
        $file_tmp = $_FILES['file']['tmp_name'][$key];
        $file_type= $_FILES['file']['type'][$key];  

        $extension = end($temp);

        if ((($_FILES["file"]["type"][$key] == "image/gif")
        || ($_FILES["file"]["type"][$key] == "image/jpeg")
        || ($_FILES["file"]["type"][$key] == "image/jpg")
        || ($_FILES["file"]["type"][$key] == "image/pjpeg")
        || ($_FILES["file"]["type"][$key] == "image/x-png")
        || ($_FILES["file"]["type"][$key] == "image/png"))
        && ($_FILES["file"]["size"][$key] < 2000000)
        && in_array($extension, $allowedExts))
        {
            //echo "<img src=upload/" . $file_name . ">";
            $images = $images . "<img src=upload/" . $file_name . "><br><a href='upload_file.php?deleteid=$file_name'>delete</a>";
            move_uploaded_file($file_tmp, "upload/".$file_name);
            echo "Uploaded!";
        }
        else
        {
            echo "Invalid file type!"; 
        }
    }
}
?>

<?php
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {

    $image_to_delete = $_GET['deleteid'];

    //unlink the image from server
        // Remove The Pic -------------------------------------------
    $pictodelete = ("upload/$image_to_delete");

    if (file_exists($pictodelete)) {
        unlink($pictodelete);
    }
    header("location: upload_file.php"); 
    exit();
}
?>

<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file[]" id="file" multiple><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php echo $images; ?>
</body>
</html> 
4

0 回答 0