0

I have a dropdown list populated with files pulled from a directory using the PHP listed below that then gets displayed onto a form and am trying to figure out how I can delete them using a delete button in the form when they're selected and displayed.

<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection"  id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
<select size="1" name="CodeList" id="CodeList">
<?php
   $directory = $directory = 'users/' . $_SESSION['username'];
   $filesContents = Array();
   $files = scandir( $directory ) ;

   foreach( $files as $file )
  {
   if ( ! is_dir( $file ) )
  {
   $filesContents[$file] = file_get_contents($directory , $file);
  echo "<option>" . $file . "</option>";
  }
   }
   ?>
   </select>

Below is the coding I was staring to use and the error beneath it that I received.

    <?php
          $dirpath = $directory = 'users/' . $_SESSION['username'];
          $file_to_delete = $_GET['$file'];
            if ( unlink ($dirpath.'/'.$file_to_delete) ) {
          echo $file_to_delete . " deleted.";
        } else {
            echo "Error.";
        }
     ?>

EDIT for Testing Attempting to use this php it returned the error:

Warning: unlink(users//) [function.unlink]: Is a directory in /home/revo/public_html/evo/avdeleteprocess.php on line 4
Error.

So it appears it found the users folder however it didn't find the file that was pulled from the dropdown.

2
Contributors
2
Replies
43
Views
2 Years
Discussion Span
Last Post by diafol
0

I can't edit my main post but updated my script which now finds the proper folder and username but not the file from the dropdown list's current selection.

<?php
//  If a session already exists, this doesn't have any effect.
session_start();
      $directory = $directory = 'users/' . $_SESSION['username'];
      $file_to_delete = $_POST['$file'];
        if ( unlink ($directory.'/'.$file_to_delete) ) {
      echo $file_to_delete . " deleted.";
    } else {
        echo "Error.";
    }
 ?>
0

Always do a file_exists() before moving/copying or deleting a file.

session_start();
$dir = 'users/' . $_SESSION['username']; 

/*assuming username doesn't violate directory naming - or maybe it's slugified? If so, beware of collisions as "diafol" and "diâfol" may be distinct usernames but they will have the same slug ("diafol"). Using a username as directory name can be problematic - so the unique id is sometimes better e.g. users/34, users/596 etc. ANyway...*/

$file = $_POST['file']; 

//note no $ in front of 'file' - or is this supposed to be there?

$path = $dir . '/' . $file;

if(file_exists($path))
{
    if(unlink($path))
    {
        echo "$path deleted";
    }else{
        echo "$path exists, but could not be deleted";
    }
}else{
    echo "$path does not exist";
}

Oh as an afterthought - check the chmod status of your files.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.