Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Looking for PHP unlink() Help [SOLVED]

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.

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.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top