[英]Deleting a file(s) from server PHP - Permission denied unlink
我想从我的 beaglebone 上运行的本地服务器中删除文件。
我创建了一个页面来显示所有文件并让您选择要删除的文件。 (如下图所示)
网页以数组的形式返回要删除的文件名给php脚本unlink.php
Unlink.php 的代码是:
<?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"; } } }?>
但是,每当我尝试删除文件时:它会失败。
unlink() php 函数没有正确实现,我不确定为什么。 我该如何以正确的方式做到这一点?
index.html
页面位于/var/www/html
中,日志位于/var/www/html/Logs/
中。 本地服务器地址为192.168.7.2
表单代码:
<?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); ?>
编辑:PHP 显示错误
警告:chmod():第 17 行的 /var/www/html/unlink.php 中不允许操作chmod($path. $files[$i], 0755);
警告:取消链接(/var/www/html/Logs/2017.01.24--13.43.43--0.log): if (unlink($path. $files[$i]))
但是当我检查ls -la
for /Logs -> 它显示为属于www-data
。 我如何更改超出此范围的权限?
<?php
$files = $_POST['file'];
$path = "/var/www/html/Logs/";
print_r($files);
if (empty($files)) {
echo "No files were selected. Go back to 192.168.7.2 and refresh the page." ;
} else {
foreach ($files as $file) {
if (unlink($path . $file)) {
echo $path. $file ." : Deleted";
} else {
echo $path. $file . " : fail";
}
}
}
?>
应该这样做, $path
是错误的,即使您使用了与其他代码中相同的方法,它也缺少斜线。 尽管如此,此代码不会阻止恶意用户操作。 如何避免PHP中的UNLINK安全风险?
从您更新的问题中,我可以看到 Logs 文件夹是/media/card/Logs
的符号链接。 ls -la /media/card
的输出是什么?
一些进一步阅读:
unlink()
/ delete 的文件没有被打开。 如果文件是.exe
等,请同时检查Windows 任务管理器 -> 进程,然后将其终止。在你确定文件没有被打开1之后,让我们试试这个代码:
// Check existence of file
if (file_exists($cekFile1)) {
// make sure you have permission to delete file
if(chmod($fileDir, 0777)){
if(!unlink($cekFile1)){
echo "unlink is fail !";
}
}else{
echo "chmod is fail";
}
// owner have read, write, execute rights.
// owner's user group only have read rights.
// everybody else only have read rights.
chmod($fileDir, 0744);
}
有关chmod()
的更多信息,请查看此参考资料:
php.net 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.