Я решил эту проблему с предупреждением, возвращаясь к if(!file_exists($file_to_delete)
(как я уже знаю, в этой папке есть только изображения, которые мне просто нужны, чтобы пользователь не мог попасть в другие каталоги) I также сделали проверку по идентификатору, что его числовое & существует в БД и продезинфицировать в запросе я считаю, могли бы вы, пожалуйста, посмотрите, хотя новый код ниже и посмотреть, если все в порядке, или если и дальнейшие проблемы существуютphp delete from sql и directory form
Большое спасибо
Heres мой код
<?php
// Include Databse
include ("common.php");
// VARIBLES \t
$delete = $_POST['delete'];
$id = $_POST['id'];
$filename = $_POST['filename'];
$ext = end(explode('.',$filename));
// Check if form has been submitted
if (isset ($delete))
{
// Check filename is not empty
if(empty($filename)) {
$status = "Please enter a FILENAME" ;
\t $error = true;
\t $filecheck = false;
}
else {
\t $filecheck = true;
}
if ($filecheck)
{
//Check user stays in correct directory & check image ext
if(!preg_match('/^\/?[\w\s-_]+\.(jpe?g|gif|png|bmp)$/',strtolower($filename)))
{
\t $error = true;
\t $status = "Please check FILENAME";
}
else {
$file_to_delete = 'images/' . $filename;
}
// Check file_to_delete is set
if ($file_to_delete)
{
// Checks the file exists
if(!file_exists($file_to_delete))
{
$status = "File not found please check FILENAME";
$error = true;
$idcheck = false;
}
else
{
$idcheck = true; \t
}
}
// Check $idcheck is set
if($idcheck)
{
// Check ID is not empty
if(empty($id)) {
$status = "Please enter a ID " ;
\t $error = true;
\t $filecheck = false;
}
//Check if ID is not numeric
else if(!is_numeric($id))
{
\t $error = true;
\t $status = "Please check ID";
}
else
{
// Check ID exists in database
$query = "SELECT id FROM `test` WHERE `id` = :id" ;
$stmt = $db->prepare($query);
$stmt->bindParam(":id", $id);
$stmt->execute();
//if ID exists.
if($stmt->rowCount() > 0)
{
\t $error = false;
}
else {
\t $error = true;
\t $status = "Please check ID";
}
}
}
}
if (!$error)
{
// Run Query & Delete File Information From Database
$query = "DELETE FROM `test` WHERE `id` = :id" ;
try {
$stmt = $db->prepare($query);
\t \t $stmt->bindParam(':id', $id);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to delete image: Please report issue to admin");
}
\t
// Delete File From Directory
unlink($file_to_delete);
$status = "File Deleted";
}
}
?>
<?php
$query = "SELECT id,photo FROM test";
try
{
// Run Query To Show The Current Data In Database
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: Please report issue to admin");
}
$rows = $stmt->fetchAll();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Delete Image</title>
<style type="text/css">
.table {
\t text-align: center;
}
.table {
\t font-weight: bold;
}
</style>
</head>
<body>
<form action="delete.php" method="post" enctype="multipart/form-data" class="table">
Please enter the Filename and ID of the image you wish to delete
<table width="178" align="center">
<tr class="table">
<td width="144" class="table">Filename</td>
<td width="30" class="table">ID </td>
</tr>
<tr>
<td><input name="filename" type="text" value="<?php echo $filename; ?>" /> </td>
<td><input name="id" type="text" id="id" value="<?php echo $id; ?>" size="3" maxlength="4" /> </td>
</tr>
</table>
<p><?php echo $status; ?><br />
<input type="submit" value="Delete Selected Image" name="delete" />
</p>
<p>IMAGE DETAILS </p>
<table width="400" align="center" class="table">
<tr>
<th width="61">ID</th>
<th width="185">Filename</th>
<th width="138">Image</th>
</tr>
</table>
<table width="400" align="center" class="table">
<?php foreach($rows as $row): ?>
<tr>
<td width="61"><?php echo $row['id']; ?></td>
<td width="185"><?php echo $row['photo']; ?></td>
<td width="138" height="138">
<img src="images/<?php echo $row['photo'] ; ?>" width="138" height="138" /></td>
</tr>
<?php endforeach; ?>
</table> </p>
<p><br />
<br />
</p>
</form>
</body>
</html>
имя файла расширения не являются надежными. если вы используете getimagesize(), то вы можете прямо проверить, какой имидж есть там ... –