I figured it out. Apparently !unlink() doesn't work as well as I anticipated. My current code is less sophisticated than I would like, but it works!
Does anyone here know a good bit about PHP? I am having trouble with unlink(), and am really baffled why my code is not functioning. I am relatively new at PHP programming and any help is appreciated.
When a file is uploaded, the file is moved to the "../uploads/" folder and the following is stored in a MYSQL table: id, date, name, description. The id is an auto increment, the date is curtime(), the name is the file name with extension, and the description is optional text.
The gist of my unlink() code is as follows:
The server checks if the id exists and that it is numeric.
Establishes the MYSQL query into variable $query.
Establishes location of the file to delete in variable $filename.
Checks if the file exists (with $filename), and informs admin if it doesn't.
If the file exists, it prompts admin with a delete confirmation and displays file.
If the deletion is confirmed, the server attempts to unlink $filename.
If the deletion is not possible the admin is informed.
If the file is unlinked, the table entry is erased in the database.
The code processes until the unlink() function, and stops at line 36 (print '<p style="color:red;">Could not delete the photo.</p>'.
Help me HBGames.org. You are my only hope.
Does anyone here know a good bit about PHP? I am having trouble with unlink(), and am really baffled why my code is not functioning. I am relatively new at PHP programming and any help is appreciated.
When a file is uploaded, the file is moved to the "../uploads/" folder and the following is stored in a MYSQL table: id, date, name, description. The id is an auto increment, the date is curtime(), the name is the file name with extension, and the description is optional text.
The gist of my unlink() code is as follows:
The server checks if the id exists and that it is numeric.
Establishes the MYSQL query into variable $query.
Establishes location of the file to delete in variable $filename.
Checks if the file exists (with $filename), and informs admin if it doesn't.
If the file exists, it prompts admin with a delete confirmation and displays file.
If the deletion is confirmed, the server attempts to unlink $filename.
If the deletion is not possible the admin is informed.
If the file is unlinked, the table entry is erased in the database.
Code:
<?php
$dbc = mysql_connect('localhost', 'admin', 'password');
mysql_select_db('database', $dbc);
if (isset($_GET['id']) && is_numeric($_GET['id']) ) {
$query = "SELECT name FROM photos WHERE id={$_GET['id']}";
$filename = "/home6/willoww3/public_html/uploads/" . $row['name'];
if (!file_exists($filename)) {
print '<p>File does not exist.</p>';
} else {
if ($result = mysql_query($query, $dbc)) {
$row = mysql_fetch_array($result);
print '<form action="delete_file.php" method="post">
<p>Are you sure you want to delete this photo?</p>';
print '<img src="../uploads/'. $row['name'] . '"><br />';
print '<input type="hidden" name="id" value ="' . $_GET['id'] . '" />
<input type="submit" name="submit" value="Delete" /> <a href="../admin/admin_files.php"><input type="button" name="cancel" value="Cancel" /></a></p>
</form>';
} else {
print '<p style="color:red;">Could not retrieve this photo because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was ' . $query . '</p>';
}
}
} elseif (isset($_POST['id']) && is_numeric($_POST['id'])) {
if (!unlink($filename)) {
print '<p style="color:red;">Could not delete the photo.</p>';
} else {
$query = "DELETE FROM photos WHERE id={$_POST['id']} LIMIT 1";
$result = mysql_query($query, $dbc);
if (mysql_affected_rows($dbc) == 1) {
print '<p>The photo has been deleted.</p>';
} else {
print '<p style="color:red;">Could not delete the photo because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}
}
} else {
print '<p style="color:red;">This page has been accessed in error.</p>';
}
mysql_close($dbc);
?>
The code processes until the unlink() function, and stops at line 36 (print '<p style="color:red;">Could not delete the photo.</p>'.
Help me HBGames.org. You are my only hope.