how do i do this? (php)

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
suppose i have 10 files in order(ie 1.txt,2.txt,3.txt,4.txt.......10.txt) and i want to delete all 10 files at once

how do i do it using loops

just change the below script if possible
<?
PHP:
for($i=1;$i<=10;i++)
{
unlink("$i.php");
}

?>
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
There doesn't appear to be anything wrong with that code, the only problem may be that the unlink() function may not be enabled for security reasons.

I would write it like this.
PHP:
<?php

for($i=1; $i<=10; i++)
{
   unlink($i.".php");
}

?>
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
suppose i have 10 files in order(ie 1.txt,2.txt,3.txt,4.txt.......10.txt) and i want to delete all 10 files at once

how do i do it using loops

just change the below script if possible
<?
PHP:
for($i=1;$i<=10;i++)
{
unlink("$i.php");
}

?>

If that's the exact php code you're using, then you're missing the $ in $i++. If not, are you sure the files exist? You use '.php' in the code, but you mentioned '.txt'. Also, if these files need to be created every so often with new data, then you might want to consider leaving them and just fopen()'ing them in w or w+ mode. That way, they'll automatically be truncated to 0 length before you write new data to them.
 

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
sorry for the mistakes i made above its $i++ and .txt
i just want to know is there a another way of deleting 10 files atonce
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
If they're all in the same directory and there are no other files in there other than them, then you can use rmdir() to remove the entire directory.
 
Top