zero-perfoliate
zero-perfoliate

Author Topic: Deleting Files from Directory HELP!!!  (Read 205 times)

Offline mdversion1

  • New PHP Members
  • Posts: 2
  • Karma: +0/-0
Deleting Files from Directory HELP!!!
« on: May 07, 2010, 07:08:28 AM »
I NEED HELP!!

I have this script that lists the files in a directory nicely with checkboxes to remove unwanted files but when I click the "Remove" button nothing happens. It won't delete anything. I think the problem lies in the array that unlinks the files. Any help is greatly appreciated.

Here's the code:

<?php

// change this to your path (NO TRAILING [ / ])

$dir = "/path/to/dir/files";
?>

 
<form action="view_image_list.php" method="post">
<table border=0 cellpadding=2 cellspacing=0 width=380 align=center>
<tr>
<td width=40 align=center valign=middle bgcolor=336699 nowrap height=21>
&nbsp;</td>
<td width=240 valign=middle bgcolor=336699 nowrap>
<font size=2 color=FFFFFF><B>File Name</B></font></td>
<td width=80 align=center valign=middle bgcolor=336699 nowrap>
<font size=2 color=FFFFFF><B>File Size</B></font></td></tr>

<?php

// Unlink Array
if ((submit)&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn"; 
unlink($remove);
}
}

// List Files in Directory
$handle=opendir($dir); 
while (($file = readdir($handle))!== false){ 
    if ($file != "." && $file != "..") {
 $size = filesize("$dir/$file");
$list .= '<tr><td width="40" align="center" valign="middle" bgcolor="93BEE"2 nowrap height="21">';
$list .= '<input type="checkbox" name="fid[]" value="'.$file.'"></td>';
$list .= '<td width="240" valign="middle" bgcolor="93BEE2" nowrap>';
$list .= '<font size="2" color="336699">'.$file.'</font></td>';
$list .= '<td align="center" width="80" valign="middle" bgcolor="93BEE2" nowrap>';
$list .= '<font size="2" color="336699">'.$size.' Kb</font></td></tr>';
    } 
}
closedir($handle);
echo $list;
?>
<tr>
<td colspan="3" align="center" valign="middle" bgcolor="336699" nowrap height="21">
<font size="2" color="FFFFFF"><input type="submit" value="Remove"></font></td></tr></table>
</form>


Thanks,

M-

Offline mdversion1

  • New PHP Members
  • Posts: 2
  • Karma: +0/-0
Re: Deleting Files from Directory HELP!!! - Problem Solved
« Reply #1 on: May 07, 2010, 09:00:09 AM »
Problem solved for anyone who likes :

End results :

<?php

// change this to your path (NO TRAILING [ / ])

$dir = "/path/to/dir/files";
?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=0 cellpadding=2 cellspacing=0 width=380 align=center>
<tr>
<td width=40 align=center valign=middle bgcolor=336699 nowrap height=21>&nbsp;
</td>
<td width=240 valign=middle bgcolor=336699 nowrap>
<font size=2 color=FFFFFF><B>File Name</B></font></td>
<td width=80 align=center valign=middle bgcolor=336699 nowrap>
<font size=2 color=FFFFFF><B>File Size</B></font></td></tr>
<?php

$fid= $_POST['fid'];

if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn"; 
unlink($remove);
}
}
$handle=opendir($dir); 
while (($file = readdir($handle))!== false){ 
    if ($file != "." && $file != "..") {
 $size = filesize("$dir/$file");
$list .= '<tr><td width="40" align="center" valign="middle" bgcolor="93BEE2" nowrap height="21">';
$list .= '<input type="checkbox" name="fid[]" value="'.$file.'"></td>';
$list .= '<td width="240" valign="middle" bgcolor="93BEE2" nowrap>';
$list .= '<font size="2" color="336699">'.$file.'</font></td>';
$list .= '<td align="center" width="80" valign="middle" bgcolor="93BEE2" nowrap>';
$list .= '<font size="2" color="336699">'.$size.' Kb</font></td></tr>';
    } 
}
closedir($handle);
echo $list;
?>
<tr>
<td colspan="3" align="center" valign="middle" bgcolor="336699" nowrap height="21">
<font size="2" color="FFFFFF"><input type="submit" value="Remove"></font></td></tr></table>
</form>