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. Lets get started.
Unlink in PHP
The unlink function has been around since PHP4. It also works on PHP5, PHP7 and PHP8. Here is a basic example of using PHP to create a file, then delete it. The following code will create a file called test.html that has the H1 tag in it with "Hello World" then it immediately deletes it using the unlink function.<?php $fh = fopen('test.html', 'a'); fwrite($fh, '<h1>Hello world!</h1>'); fclose($fh); unlink('test.html'); ?>
Removing Directories in PHP
Alternatively, you can remove and entire directory in PHP. Check out the example code below from php.net. Here we check if the directory "uploads" is created, if not then we create it. We use this in our script to make our uploads' folder. However, in the script below we use the "rmdir" function to delete the directory.<?php if (!is_dir('uploads')) { mkdir('uploads'); } rmdir('uploads'); ?>OK lets get on with the script to upload and delete files.
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>