I have resolved this issue of the warning by going back to if(!file_exists($file_to_delete)(as I already know in that folder is an is only images I just needed it so user could not get to other directories) I have also made a check on the id that its numeric & exists in db and sanitised the query's I believe could you please have a look though the new code below and see if ok or if and further problems exist
Many thanks
Heres my code
<?php
// Include Databse
include ("common.php");
// VARIBLES
$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" ;
$error = true;
$filecheck = false;
}
else {
$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)))
{
$error = true;
$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;
}
}
// Check $idcheck is set
if($idcheck)
{
// Check ID is not empty
if(empty($id)) {
$status = "Please enter a ID " ;
$error = true;
$filecheck = false;
}
//Check if ID is not numeric
else if(!is_numeric($id))
{
$error = true;
$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)
{
$error = false;
}
else {
$error = true;
$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);
$stmt->bindParam(':id', $id);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to delete image: Please report issue to admin");
}
// 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 {
text-align: center;
}
.table {
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>