• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / PHP / php Delete Files with php Unlink Function

php Delete Files with php Unlink Function

December 18, 2021 Leave a Comment

To delete a file using php, the easiest way is to use the php unlink() function.

unlink("./folder/file_name1.txt");

Deleting files with php is easy. We can use the php unlink() function to delete files

If we want to delete a file named “file_name.txt”, we can do this with the following php code:

unlink("./folder/file_name.txt");

Deleting Multiple Files with php

In php, it is easy to delete multiple files in a folder. To do so, we can use the php unlink() function in a loop.

Let’s say we have 5 files and we want to delete these 5 files.

We can put the 5 file names in an array, and then loop over that array calling the unlink() function inside the loop:

$files_to_delete = array("file1.php","file2.php","file3.php","file4.php","file5.php");

for ($x = 0; $x < 5; $x++) {
  unlink("./folder/" . $files_to_delete[$x]);
}

With this code, the 5 files will all be deleted.

Hopefully this article has been helpful for you to understand how you delete a file or delete multiple files using php.

Other Articles You'll Also Like:

  • 1.  php max – Find Maximum Value of Array in php
  • 2.  echo php – Output One or More Strings
  • 3.  php array_key_exists() Function – Check if Key Exists in Array
  • 4.  In PHP isset Method is Used For Checking if Variable is Defined
  • 5.  php array_fill() – Create Array of Length N with Same Value
  • 6.  php cos – Find Cosine of Number in Radians Using php cos() Function
  • 7.  Remove String from String in php with str_replace()
  • 8.  Remove Duplicates from Array in php with array_unique()
  • 9.  php array_map() – Create New Array from Callback Function
  • 10.  php is_numeric – Check if Variable is a Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy