[英]file not deleting form folder php
我正在嘗試從上載文件夾中刪除文件,但是代碼不起作用。
它是從數據庫中刪除行,而不是從文件夾中刪除文件。
<?php
extract($_REQUEST);
include_once('include/dbcon.php');
$dell= $_GET['del'];
$sql1=mysqli_query($conn, "SELECT * FROM notes WHERE notes_id='$dell'");
$row1=mysqli_fetch_array($sql1);
$delfile= $row1["file_path"];
unlink("uploads/".$delfile);
mysqli_query($conn, "DELETE FROM notes WHERE notes_id='$dell'");
header("location: deshboard.php");
?>
上載文件夾中的讀取和寫入權限可能是問題。
要檢查文件許可權,可以使用以下命令檢查文件是否可寫:
//Make sure to always get the full file path from $delfile
$delfile = /* path to the uploads folder */ . $row1["file_path"];
if (is_writable($delfile)) {
print 'YES!!!: I am writeable';
} else {
print 'NO!!!: File not writable, please fix me';
}
要檢查文件是否可讀,只需將is_writable()
與is_readable()
交換即可
在使用文件/文件夾及其權限時,學習如何使用chmod對我很有幫助。
首先檢索文件的完整路徑:
$delfile = dirname(__FILE__) . $row1["file_path"];
然后確保文件權限設置為0777:
chmod($delfile , 0777);
最后結果:
<?php
extract($_REQUEST);
include_once('include/dbcon.php');
$dell= $_GET['del'];
$sql1=mysqli_query($conn, "SELECT * FROM notes WHERE notes_id='$dell'");
$row1=mysqli_fetch_array($sql1);
$delfile = dirname(__FILE__) . $row1["file_path"];
if (is_file($delfile )) {
chmod($delfile , 0777);
if (unlink($delfile )) {
//'File deleted';
mysqli_query($conn, "DELETE FROM notes WHERE notes_id='$dell'");
header("location: deshboard.php");
} else {
//'Cannot remove that file';
}
} else {
//'File does not exist';
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.