To delete a file or files using PHP code you can use the unlink() method.
Syntax : bool unlink ( string $fileName [, resource $context ] )
It returns a boolean TRUE if the file is deleted successfully and FALSE if it failed to delete it. The resource part is option, it was added in PHP version 5.
Let's see some examples : 1. Delete a single file using unlink()<?php
$fileName = "some-file.txt";
$filePath = "file/path/if/any/";
//delete the file
unlink($fileName.$filePath);
?>
2. Delete all text files in a directory using unlink()
<?php
$filePath = "file/path/if/any/";
foreach (glob($filePath."*.*")) as $fileName) {
//*.txt is a wild card will delete all text files contained in the directory location specified.
unlink($fileName");
echo $fileName." was deleted!";
}
?>
3. Delete all files in a directory using unlink()
<?php
$filePath = "file/path/if/any/";
foreach (glob($filePath."*.*")) as $fileName) {
//*.* is a wildcard that will delete all files contained in the directory location specified.
unlink($fileName);
echo $fileName." was deleted!";
}
?>
4. Delete all text files in a directory using an easy way!
<?php
$filePath = "file/path/if/any/";
$wildcard = "*.png"
//will delete all png files
array_map( "unlink", glob( $filePath.$wildcard ));
?>
Note :
unlink was added to PHP in version 4, hence it will not work with previous versions.
If you get a warning like Warning: unlink(uploaded_images/pdf.png): Permission denied in /Applications/XAMPP/xamppfiles/c2c/temp.php on line 7 , then its because you do not have permissions to either
access this file or the directory.
Its always good to get the boolean value returned by unlink method to check success or failure in deleting the files.More Posts related to PHP,
- Delete file using PHP code : unlink()
- PHP header location function not called
- 403 forbidden error for Image
- Call PHP function on Button click using jquery ajax
- How to Pretty Print JSON in PHP
- Step-by-Step Guide: How to Fix - Error Establishing a Database Connection in WordPress
- PHP Base64 encoding decoding a variable
- PHP Fatal error : Call to a member function bind_param() on a non-object
- PHP.ini: How to Remove URL Forward Slash Before Single or Double Quotes
- PHP drag and drop file upload tutorial using dropzone.js
- Upload Pdf file using PHP Script
- PHP Warning: Cannot modify header information - headers already sent
- macOS - cannot calculate MAC address: Using fd 9 hv_vm_create HV_ERROR [PHP XAMPP]
- PHP Code for sending Emails
- How to destroy PHP session()
- Installing vue.js in Laravel 8
- PHP 301 Redirect Permanently
- Upload docx file using PHP script
- Save current timestamp in MySQL using PHP mysqi binding
- Copy file from one directory to other in Php
- Failed to load resource: net::ERR_CACHE_MISS PHP
- PHP Script to Upload Images to Server
More Posts:
- How to come out of dquote prompt in Terminal - macOS/Linux - MacOS
- How to Change Mac Password on macOS Ventura - MacOS
- Your Android SDK is missing, out of date or corrupted SDK Problem - Android-Studio
- How to make div or text in html unselectable using CSS - CSS
- How to get weather details in Command Prompt, macOS or Linux Terminal - HowTos
- Display (Show) bookmarks bar Safari - HowTos
- Cut, Copy and Paste Keyboard Shortcuts on Mac Keyboard - MacOS
- Notepad++ Save Failed - Please check if this file is opened in another program. - NotepadPlusPlus