我试图创建一个删除上传到我服务器的图像的按钮。但是,当我上传了一张图片后,点击删除无效,它会从页面中删除图片,但是当我检查网站目录时,它仍然存在。另外,DELETION COMPLETE没有回显消息,只是DELETION FAILED,所以它让我想知道它是否有效并且只是刷新页面。不知道为什么会这样,还有一种方法可以让它只在显示图像时显示加载图像gif吗?这就是我所拥有的:
<?php
ob_clean();session_start();
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
$loadingimage = false;
if(isset($_FILES['UploadFileField'])){
$allowed = array('jpg','png','jpeg');
$name = $_FILES["UploadFileField"]["name"];
$tmp = $_FILES['UploadFileField']['tmp_name'];
$type = $_FILES['UploadFileField']['type'];
$newName = "Image Attachment.jpg";
$types = array('jpg','png','jpeg');
$ext = pathinfo($name, PATHINFO_EXTENSION);
if(in_array($ext,$types)){
move_uploaded_file($tmp, "UPLOADS/$newName");
echo '<font size="3"><p align="center"><b>UPLOAD SUCCESSFUL: </font><font color="#000000" size="3">Your document has now been uploaded and is ready to send.</b></p></font>';
$loadingimage = true;
}
else {
if(!$tmp){
echo '<font size="3"><p align="center"><b>UPLOAD FAILED: </font><font color="#000000" size="3">No document has been selected.</b></p></font>';
}
else {
echo '<font size="3"><p align="center"><b>UPLOAD FAILED: </font><font color="#000000" size="3">Uploaded document was an incorrect extension type, please use ".jpg", ".jpeg", or "png" only.</b></p></font>';
}
}
}
if (isset($_POST['submit'])){
header( 'Location: Review.php' );
}
if (isset($_POST['delete'])){
if ($loadingimage == true){
echo '<font size="3"><p align="center"><b>DETLEION COMPLETE: </font><font color="#000000" size="3">Image no longer available</b></p></font>';
unlink('UPLOADS/Image Attachment.jpg');
$loadingimage = false;
}
else{
echo '<font size="3"><p align="center"><b>DETLEION FAILED: </font><font color="#000000" size="3">No image available for deletion</b></p></font>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Attach Image</title>
<link href="CSS/boilerplate.css" rel="stylesheet" type="text/css">
<link href="CSS/master.css" rel="stylesheet" type="text/css">
<script src="JAVASCRIPT/respond.min.js"></script>
</head>
<body link="black">
<div class="gridContainer clearfix">
<div id="borderDiv">
<div id="navDiv">
<div id="backNavDiv">
<a href="CompletedBy.php"><font color="white"><p align="left"><b> < Completed By</b></p></font></a>
</div>
<div id="logoutDiv">
<a href="index.php?logout"><font color="white"><p align="right"><b>Log Out > </b></p></font></a>
</div>
</div>
<div id="headerDiv">
<p>Attach Image</p>
</div>
<?php
if($loadingimage == false){
echo '<div id="imageDiv"><img src="IMAGES/loading.gif"></div>';
}
else{
echo '<div id="imageDiv"><img src="UPLOADS/Image Attachment.jpg"></div>';
}
?>
<div id="loginBtnDiv">
<div id="uploadAreaDiv">
<form action="AttachImage.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
<label for="UploadFileField"></label>
<input type="file" name="UploadFileField" id="UploadFileField"/>
<input type="submit" name="UploadButton" id="UploadButton" value="Upload"/>
</form>
</div>
<form action="AttachImage.php" method="post" enctype="multipart/form-data" name="delete" id="delete">
<input id="delete" name="delete" type="submit" value="Delete">
</form>
<form action="AttachImage.php" method="post" enctype="multipart/form-data" name="FileForm" id="FileForm">
<input id="submit" name="submit" type="submit" value="Next">
</form>
</div>
</div>
</div>
<div id="logoDiv">
<img src="IMAGES/Logo.png">
</div>
</body>
</html>
答案 0 :(得分:1)
您的$loadingimage
变量始终为false。所以你永远不会执行unlink()
函数。如果您认为它是真的,因为您在true
中将其设置为if(isset($_FILES['UploadFileField'])){}
,则不会,因为您的网页已重新加载并且您已将$_POST['delete']
传递到该页面并{{1 }}变量将再次设置为false。所以删除条件
$loadingimage
并且您的代码应该有效。我完全没有看到你为什么会这样做的原因。
答案 1 :(得分:1)
正如我之前评论的那样,因为您定义了$loadingimage = false;
,所以如果文件同时上传,您将只能删除该文件 - 这似乎毫无意义。
您应该检查文件是否存在,而不是使用$loadingimage
变量来取消链接文件。
你可以改变
if ($loadingimage == true){
带
if (file_exists("UPLOADS/Image Attachment.jpg")) {
这只会删除文件(如果存在),并阻止unlink
引发警告。