how to delete files from root dir.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

how to delete files from root dir.

Post by itsmani1 »

i want to delete all .html and .htm files from root directory
how can i do so?

one morething is
i want to convert my php extension to html for example
http://maair.net/content.php i want to convert it to .html but it should work as it is working Dynamically

thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

glob() or opendir()-readdir() to build a file list, unlink() to delete. Be aware: it will not be able to delete files it doesn't have permission to.

You have to add .html as a php parsed type then rename the file. Be aware that all .html files, whether they have php in them or not, will be processed by php.
SKDevelopment
Forum Newbie
Posts: 13
Joined: Thu Jan 26, 2006 10:42 am

Re: how to delete files from root dir.

Post by SKDevelopment »

It would be advisable also to replace the unlink() command with echo and see which files your script is going to delete before you do any actual deleting.

--
Best Regards,
Sergey Korolev
www.skdevelopment.com
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

well, actually i want to delete all .html files
is there any command or function by calling which i can delete all .html files in a specfic directory

thanks.
Mannan.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

one small question!

how can i selete all files in a specfic directory, means i want to select all files in an array etc how can i do so?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

feyd wrote:glob() or opendir()-readdir() to build a file list, unlink() to delete.
Look in these sections of the PHP manual. There are good examples there.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

echo ` rm -rf /path/*.html`;
be careful with that one :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

using apache?

you could use a mod rewrite (assuming all .html files are .php) and give the illusion that it's an .html file (don't know if that's what you're going for).

.htaccess file

Code: Select all

RewriteEngine On

RewriteRule ^(.+)\.html$ $1\.php
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Code: Select all

$dir = "../";
if (is_dir($dir)) {
   if ($dh = opendir($dir))
   {
		foreach (glob("*.html") as $filename)
		{
			unlink($filename);
		}
   }
}
directory strucure is like this:
abc/xyz

i am in xyz rign now and want to delete all .html files of abc
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

itsmani1 wrote:

Code: Select all

$dir = "../";
if (is_dir($dir)) {
   if ($dh = opendir($dir))
   {
		foreach (glob("*.html") as $filename)
		{
			unlink($filename);
		}
   }
}
directory strucure is like this:
abc/xyz

i am in xyz rign now and want to delete all .html files of abc
This won't work if the files were uploaded by FTP. You'll need to CHMOD them first.

Note your path to unlink is wrong. You need to prepend the $dir too.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

psst, glob() returns full path d

;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:psst, glob() returns full path d

;)
:oops: Do check the permissions though itsmani
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

well files are not uploaded via ftp, there are generated via script.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

my main question is how do i move the directory which parent of current direcoty
for example i am in xyz dir which is like this ../abc/xyz
i want to move to ../abc how do i move using golb()
Post Reply