Delete Files in Directory or Folder With PHP | How To Tutorial | Unlink

Script To Delete Files in PHP

You can take this script and upload it to any directory on your server. If it does not have an uploads directory, it will make one for you. It will then scan the uploads directory and display them on your web page. You can then delete each file and the page will refresh itself. You can download it here. index

<!DOCTYPE html>
<html lang="en">
<head>
<title>Unlink in PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>

</head>
<body>

<div class="container">
<div class="row" >
<?php 
//create uploads folder
if (!is_dir('uploads')) {
    mkdir('uploads');
}
//delete the file 
if(isset($_POST['delete'])){
  $file=$_POST['fname'];
  $pathtodir = getcwd();
  $dir = $pathtodir.'/uploads/';
  unlink($dir."".$file);
  echo '<div class="alert alert-success" role="alert">
      Success, Your file has been deleted. 
    </div>';
    echo "<meta http-equiv='refresh' content='0'>";
  
}
$path    = 'uploads';
$files = scandir($path);
$files = array_diff(scandir($path), array('.', '..'));
foreach($files as $newvar){
  $ext = pathinfo($newvar, PATHINFO_EXTENSION);
  if($ext=='pdf' || $ext=='zip' || $ext=='docx'|| $ext=='txt'|| $ext=='odf'|| $ext=='sql' ){
    echo '<div class="card" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">'.$newvar.'</h5>
    <h6 class="card-subtitle mb-2 text-muted">'.$ext.' file.</h6>
    <p class="card-text">Delete file by clicking below.</p>
    <form method="post" onSubmit="window.location.reload()">
    <input type="hidden" name="fname" value="'.$newvar.'">
    <input type="submit" name="delete" value="Delete" class="btn btn-danger">
    </form>
  </div>
</div>';
  }else{
    echo '<div class="card" style="width: 18rem;">
  <img class="card-img-top" src="uploads/'.$newvar.'" alt="Card image cap">
  <div class="card-body">
    <p class="card-text">File name: '.$newvar.'</p>
  <form method="post">
    <input type="hidden" name="fname" value="'.$newvar.'">
    <input type="submit" name="delete" value="Delete" class="btn btn-danger">
    </form>
  </div>
</div>';
}
}
?></div></div>
<?php
//upload file
if(isset($_POST['submit'])){
if(file_exists($_FILES['file']['tmp_name'])) {
$errors= array();
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
$fileend=explode('.',$file_name);
$file_ext=strtolower(end($fileend));

$random1 = substr(number_format(time() * rand(),0,'',''),0,10); 

$file_name= $random1 . $_FILES['file']['name']; 

$extensions= array("jpeg","jpg","png","jfif","pdf","zip","txt","odf","docx","sql");

if(in_array($file_ext,$extensions)=== false){
$errors[]='<div class="alert alert-warning">extension not allowed, please choose a JPEG, JFIF or PNG file.</div>';
}

if($file_size > 2097152){
$errors[]='<div class="alert alert-waring">File size must be under 2 MB</div>';
}

$file_name = str_replace(' ', '-', $file_name);

if(empty($errors)==true){
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo '<div class="alert alert-success">Success: File Uploaded!</div>';
echo "<meta http-equiv='refresh' content='0'>";
}else{
print_r($errors);
}
}else{
  $file_name="None";
}
}

?>
<br/>
<div class="container-fluid">
<h1>Upload File</h1>
<form id="show" method="post" action="" enctype="multipart/form-data">
<label class="form-label" for="picture">Upload File</label>
<input type="file" class="form-control" id="file" name="file"  />
<input id="submit" name="submit" type="submit" class="btn btn-success" value="Upload File">
</form>
</div>
</div>
<footer class="page-footer font-small blue">
  <div class="footer-copyright text-center py-3">© 2022 Copyright:
    <a href="https://a1websitepro.com"> A1WebSitePro.com</a>
  </div>
</footer>
</body>
</html>

Delete Files in Directory or Folder With PHP | How To Tutorial | Unlink was last modified: October 22nd, 2022 by Maximus Mccullough
Summary
Delete Files in Directory or Folder With PHP | How To Tutorial | Unlink
Article Name
Delete Files in Directory or Folder With PHP | How To Tutorial | Unlink
Description
To delete files in a directory or folder with PHP, all you have to do is use the unlink function. Unlink not only deletes pictures, but will delete any kind of file in a directory.
Author
Publisher
A1WEBSITEPRO LLC
Logo
Delete Files in Directory or Folder With PHP

Pages:Previous 1 2

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.