Use the same array in multiple PHP pages



In one PHP page I collect some data on the server (i.e. a list of the file in a folder) and show it into a table. The user may require to delete one file. Because I'm new to PHP I don't know if my approach is correct. Let's see some code:


$path = "/path/to/directory/";
$files = array_diff(scandir($path), array('.', '..'));

<?php for ($i = 0; $i < count($files); $i++) { ?>
<td class="d-none"><?php echo "{$i}"?></td>
<td><?php echo "{$files[$i]}"?></td>
<button type="button" class="btn btn-danger btn-sm delete"><i class="fas fa-trash-alt"></i></button>
<?php } ?>


$(document).on("click", ".delete", function(e) {
var id = $(this).closest("tr").find('td').eq(0).html();
var filename = $(this).closest("tr").find('td').eq(1).html();

url: "delete.php?id=" + id,
type: 'DELETE',
success: function(result) {


$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);
$id = $params["id"];
$path = "/path/to/directory/";
//$filename = ???;
//unlink($path . $filename);
echo "success";

Some options I thought:

  1. Instead of the id pass the filename as parameter of the DELETE query. I guess it's not a good way.
  2. list.php should place the file names into a db, so I can access them everywhere. Actually I did it, on a "live" db in /dev/shm but I wonder if it's a valid approach. Every time the page reloads I need to drop the table and create it again (on the page there's a refresh button for this purpose)
  3. use the $_SESSION variable to store the array so the delete.php page can retrieve the filename by its id.

Please note, I'm not asking for an opinion about the three options. I'm asking what is the right approach from a technical point of view: safety, performances, reliability, etc...

Continue reading...