deleting file with unlink()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsjoes
Forum Commoner
Posts: 41
Joined: Thu May 20, 2010 3:15 pm

deleting file with unlink()

Post by dsjoes »

the code below creates a table that lets me choose a row to delete from mysql database( it all works no problems)

Code: Select all

<?php
$host="host"; // Host name 
$username="user"; // Mysql username 
$password="pass"; // Mysql password 
$db_name="testdocs"; // Database name 
$tbl_name="Docs"; // Table name 
       

        // Connect to server and select databse.
        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name")or die("cannot select DB");
       
        // Build SQL query
        if(!isset($_POST['delete'])) $sql="SELECT * FROM $tbl_name ORDER BY id";
        else {
                $sql = "DELETE FROM $tbl_name WHERE";

                // add row id to where section
                for($i=0;$i<count($_POST['checkbox']);$i++){
                        if($i != 0) $sql.= "AND ";
                        $sql .= " id='" . $_POST['checkbox'][$i] . "'";
                }
     }

      $result = mysql_query($sql);
       if(isset($_POST['delete']))  header('Location: index.php'); // redirect
?>
<table  align="center" width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="delete" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table width="400" border="1" cellpadding="3" cellspacing="1">
<tr>
<td colspan="5" align="center"><strong>Testimonials</strong> </td>
</tr>
<tr>
<td align="center"><strong>Select</strong></td>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Message</strong></td>
<td align="center"><strong>Download</strong></td>
</tr>
<?php while($rows=mysql_fetch_array($result)){ ?>
        <tr>
                <td align="center">
                        <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>">
                </td>
                <td><? echo $rows['id']; ?></td>
                <td><? echo $rows['Name']; ?></td>
                <td><? echo $rows['Message']; ?></td>
                <td><? echo $rows['Download']; ?></td>
        </tr>
        <tr>
        <td colspan="5" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td>
        </tr>
<?php  }
        mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
but what i am trying to do is add unlink() to the script so when i delete a row it deletes the linked file.
this bit is where the filename is shown like test.doc and the files are stored in the directory /test_docs

Code: Select all

<td><? echo $rows['Download']; ?></td>
is there anyway to do it so it deletes the file
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: deleting file with unlink()

Post by mikecampbell »

Did you try

Code: Select all

unlink('/test_docs/'.$rows['Download']);
dsjoes
Forum Commoner
Posts: 41
Joined: Thu May 20, 2010 3:15 pm

Re: deleting file with unlink()

Post by dsjoes »

yes i have tryed it but it doesn't work i have even tryed it with out the /test_docs folder so that all the files were in the same place but still nothing.
:banghead:
dsjoes
Forum Commoner
Posts: 41
Joined: Thu May 20, 2010 3:15 pm

Re: deleting file with unlink()

Post by dsjoes »

anyone
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: deleting file with unlink()

Post by mikecampbell »

Have you set the permissions on that directory so you can delete files?
dsjoes
Forum Commoner
Posts: 41
Joined: Thu May 20, 2010 3:15 pm

Re: deleting file with unlink()

Post by dsjoes »

yes the permissions are set correctly.

Code: Select all

<?php
$host="xxxx"; // Host name 
$username="xxxx"; // Mysql username 
$password="xxxxx"; // Mysql password 
$db_name="testdocs"; // Database name 
$tbl_name="docs"; // Table name 
       

        // Connect to server and select databse.
        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name")or die("cannot select DB");
       
        // Build SQL query
        if(!isset($_POST['delete'])) $sql="SELECT * FROM $tbl_name ORDER BY id";
        else {
                $sql = "DELETE FROM $tbl_name WHERE";

                // add row id to where section
                for($i=0;$i<count($_POST['checkbox']);$i++){
                        if($i != 0) $sql.= "AND ";
                        $sql .= " id='" . $_POST['checkbox'][$i] . "'";
                }
     }

      $result = mysql_query($sql);
if(isset($_POST['delete'])){ 
    header('Location: index.php'); // redirect
    exit;}?>
<table  align="center" width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="delete" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table width="400" border="1" cellpadding="3" cellspacing="1">
<tr>
<td colspan="6" align="center"><strong>Testimonials</strong> </td>
</tr>
<tr>
<td align="center"><strong>Select</strong></td>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Description</strong></td>
<td align="center"><strong>Download</strong></td>
<td align="center"><strong>Last Modified</strong></td>
</tr><?php while($rows=mysql_fetch_array($result)){  
 $file_name = $rows['Download'];
unlink('/hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/docs/'.$file_name); ?>
        <tr>
                <td align="center">
                        <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>">
                </td>
                <td align="center"><?php echo $rows['id']; ?></td>
                <td align="center"><?php echo $rows['Name']; ?></td>
                <td align="center"><?php echo $rows['Message']; ?></td>
                <td align="center"><a href="/admin/docs/<?php echo $rows['Download']; ?>">Download</a></td>
                <td align="center"><?php echo $rows['Modified']; ?></td>
        </tr>
        <tr>
        <td colspan="6" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td>
        </tr>
<?php  }
        mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
this now deletes but it deletes every file in the folder instead of just the selected (every file that has got it's name in the database like test.doc) it deletes them when i refresh the page
dsjoes
Forum Commoner
Posts: 41
Joined: Thu May 20, 2010 3:15 pm

Re: deleting file with unlink()

Post by dsjoes »

bump
z0e0u0s
Forum Newbie
Posts: 1
Joined: Fri Jan 07, 2011 9:06 am

Re: deleting file with unlink()

Post by z0e0u0s »

You have to put the full path to the file if the script isn't in the same folder as the file.

http://php.net/manual/en/function.unlink.php

If you don't want to include the full path use the chdir() function then call the unlink() function with the file to be deleted.

http://us3.php.net/manual/en/function.chdir.php
dsjoes
Forum Commoner
Posts: 41
Joined: Thu May 20, 2010 3:15 pm

Re: deleting file with unlink()

Post by dsjoes »

it has got the full path

Code: Select all

unlink('/hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/docs/'.$file_name); 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: deleting file with unlink()

Post by McInfo »

I suggest you reconsider the logic of your script. Try drawing a flowchart.

Here are a few of the logic flaws. (The following snippets are pseudo-code.)

Code: Select all

if (isset($_POST['delete'])) { // Has the user clicked the "delete" button?
    // Yes; the user has clicked the button.
    exit; // Halts the script.
}
// No; the user has not clicked the button.
unlink(); // Deletes a file.

Code: Select all

while (mysql_fetch_array()) { // Loops through all selected rows.
    unlink(); // Nothing stops this from deleting a file
              // during every iteration of the loop.
}

Code: Select all

DELETE FROM docs WHERE id = 1 AND id = 2
# For what row will id be 1 AND 2 simultaneously? None.

Code: Select all

<input id="checkbox[]" />
<!-- Every id should be unique throughout the the page. -->

Code: Select all

$rows = mysql_fetch_array()
/* The function fetches one row at a time,
   so the variable name is misleading. */
Post Reply