How to delete all files from a folder using PHP

0 votes
How to delete all files from a folder using PHP?

For example I had a folder called `Temp' and I wanted to delete or flush all files from this folder using PHP. Could I do this?
Sep 14, 2020 in PHP by kartik
• 37,520 points
1,381 views

1 answer to this question.

0 votes

Hello @kartik,

Use this:

$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
  if(is_file($file))
    unlink($file); // delete file
}

If you want to remove 'hidden' files like .htaccess, you have to use

$files = glob('path/to/temp/{,.}*', GLOB_BRACE);

Hope it helps!!
ThanK you!!

answered Sep 14, 2020 by Niroj
• 82,840 points

Related Questions In PHP

0 votes
1 answer

How to copy a file from one directory to another using PHP?

Hello @kartik, You could use the copy() function : // Will ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,840 points
1,377 views
0 votes
2 answers

How to copy a file from one directory to another using PHP?

Simply, $source = 'Source_file_location' ...READ MORE

answered Oct 12, 2020 in PHP by anonymous
• 140 points
1,647 views
0 votes
1 answer

How to check if a file exists from a url using php?

Hello, You have to use CURL function does_url_exists($url) { ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,840 points
4,079 views
0 votes
1 answer

How to get the list of specific files in a directory using php?

Hello @kartik, You'll be wanting to use glob() Example: $files = ...READ MORE

answered Nov 6, 2020 in PHP by Niroj
• 82,840 points
2,231 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,994 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,691 views
0 votes
1 answer

How to read a list of files from a folder using PHP?

Try this: $files = array_values(array_filter(scandir($path), function($file) use ($path) ...READ MORE

answered Nov 18, 2020 in PHP by Niroj
• 82,840 points
607 views
0 votes
1 answer

How to zip a whole folder using PHP?

Hello @kartik, You can Zip a whole folder using ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,840 points
8,773 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP