I want to delete files from my local server that's running on my beaglebone.
I have created a page that displays all the files and lets you select the files to delete. (As you can see below)
The webpage returns the names of the files to delete in the form of an array to the php script unlink.php
The code for Unlink.php is:
<?php
$files = $_POST['file'];
print_r($files);
if (empty($files)) {
echo "No files were selected. Go back to 192.168.7.2 and refresh the page." ;
} else {
$N = count($files);
for ($i = 0; $i < $N; $i++) {
$path = '/Logs/';
print_r($path);
#chown($path, 666);
if (unlink($path . $_GET['$files[$i]'])) {
echo ": Deleted";
} else {
echo "fail";
}
}
}
?>
However, whenever I try to delete a file: It fails.
The unlink() php function isn't being implemented properly and I'm not sure why. How do I do this the right way?
The index.html
page is located in /var/www/html
and the logs are located in /var/www/html/Logs/
. The address of the local server is 192.168.7.2
Form code:
<?php
$url = $_SERVER['DOCUMENT_ROOT'];
$path = "/var/www/html/Logs";
$dh = opendir($path);
$k = 0;
$foo = True;
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
if ($k == 0 || $k % 6 == 0) {
$col .= "<tr><td><input type='checkbox' name='file[]' value='$file'> <a href='Logs/$file'>$file</a><br /></td>";
} else if ($k % 6 == 5) {
$col .= "<td><input type='checkbox' name='file[]' value='$file'> <a href='Logs/$file'>$file</a><br /></td></tr>";
} else {
$col .= "<td><input type='checkbox' name='file[]' value='$file'> <a href='Logs/$file'>$file</a><br /></td>";
}
$k++;
}
}
echo "<form action='unlink.php' method='post'><table>$col</table><br/><center><input type='submit' name='formSubmit' value='Delete' /></center></form>";
closedir($dh);
?>
EDIT: PHP display-errors
Warning: chmod(): Operation not permitted in /var/www/html/unlink.php on line 17
chmod($path . $files[$i], 0755);
Warning: unlink(/var/www/html/Logs/2017.01.24--13.43.43--0.log): Permission denied in /var/www/html/unlink.php on line 18 fail
if (unlink($path . $files[$i]))
but when I check ls -la
for /Logs -> it shows up as it belongs to www-data
. How do I change the permissions beyond this?