SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2012
    Location
    Web
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deleting file from the directory and entry from database

    Hello all,

    I have made a file upload system (storing info in database). I also added a page where user gets to see the files he/she has added. Below every file I have given an option for them to delete the file.

    My problem here is I am not able to delete the file and remove the entry from the DB too. Could you please help me with this?

    This is my my_uploads.php page where use sees the file he has uploaded with the time of upload being fetched from DB.

    PHP Code:
    <?php 
        
    include $_SERVER["DOCUMENT_ROOT"].'/TestSite/core/init.php';
        
    faculty_protect_page();
        include 
    $_SERVER["DOCUMENT_ROOT"].'/TestSite/includes/overall/header.php';
    ?>
            <center><h3>My Uploads</h3></center><br/>                       
    <?php
    $uid
    =$faculty_data['faculty_id'];
    $query="SELECT * FROM uploads";
    $result=mysql_query($query);

    while(
    $row=mysql_fetch_assoc($result))
    {
    if(
    $uid==$row['faculty_id'])
    {
    echo 
    '<form action="delete.php" method="POST">';
    echo 
    "<strong>File: </strong>";
    $url=$row['link'];
    $new="http://ankursinha.net/TestSite/fileshare/".$url
    echo 
    "<a href='$new'>$new</a><br/>";
    echo 
    "<strong>On: </strong>".$row['datetime'];
    echo 
    '<br><input type="submit" name="delete" class="btn btn" value="Delete File"/>';
    echo 
    '<hr>';
    echo 
    '</form>';
    }
    }
    ?>
    <center>List of files sorted by date</center>
    <?php include $_SERVER["DOCUMENT_ROOT"].'/TestSite/includes/overall/footer.php'?>
    And my delete.php action file which is supposed to delete the file and remove the file name and it's entries from DB:

    PHP Code:
    <?php
    include $_SERVER["DOCUMENT_ROOT"].'/TestSite/core/init.php';

    if(isset(
    $_POST['delete']))
    {
     
    unlink($new);
     
    $query="DELETE FROM uploads WHERE link = '$url'";
     
    $result=mysql_query($query);
     
    header('Location: my_uploads.php');
     exit();
     }
     else {
     echo 
    '<script type="text/javascript">alert("Oops something went wrong!")</script>';
     
    header('Location: my_uploads.php');
     exit();
     }
    ?>
    Thank you

  2. #2
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you're not doing anything with $result, you could try:

    mysql_query("DELETE FROM uploads WHERE link = '$url'")or die('Unable to remove from table '.mysql_error());
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,023
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    <center> is deprecated, and has been for a long while. Don't use it - either use inline css or even better, put a class on the elements and assign the centering to that class.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •