Jump to content

Deleting Files From a Directory


Recommended Posts

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>

 </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>

 

 

 

Link to comment
Share on other sites

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> 

</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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.