[英]Delete file from directory PHP
我已設法將我的文件上傳並下載到目錄中,我只是在刪除選項上找不到任何內容。 我寫了一些代碼,但它不起作用:這是我的文件上傳:
<?php
if(isset($_FILES['file'])){
$file = $_FILES['file'];
//file properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
//the file extension
$file_ext = explode ('.', $file_name);
$file_ext = strtolower(end($file_ext));
//allowed extensions
$allowed = array('txt', 'jpg');
if(in_array($file_ext, $allowed)){
if($file_error === 0){
//allowed size
if($file_size<= 2097152){
$file_name_new = $file_name;
$file_destination = 'files/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)){
echo '<div class="errors"> Uploaded Succesfully </div>';
}
}
}
}else echo ' <div class="errors"> Wrong file type </div>';
}
?>
這是我的顯示和下載和刪除:
<?php
$dir = "files/";
if ($opendir = opendir($dir)){
while ($fileopen = readdir($opendir)){
if ($fileopen != "." && $fileopen != ".." )
echo "<a href='$dir$fileopen' download>$fileopen</br>";
echo "<a href='deletefile.php?file=".$fileopen."'>DELETE";
}
}
else "could not open folder";
?>
這是我的deletefile.php:
$filename = $_GET['$fileopen']; //get the filename
unlink('final/files'.DIRECTORY_SEPARATOR.$filename); //delete it
header('location: upload.php'); //redirect back to the other page
?>
有誰知道如何解決它?
嘗試改變:
unlink('final/files'.DIRECTORY_SEPARATOR.$filename);
對此:
unlink('files'.DIRECTORY_SEPARATOR.$filename);
刪除時,您不在files目錄中工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.