Forum Moderators: coopster

Message Too Old, No Replies

PHP file delete issues

PHP file delete

         

galahad2

10:31 am on Jul 20, 2009 (gmt 0)

10+ Year Member



Hi, I need to be able to manually delete specific files using PHP- the files are PDF documents the links to which get generated as a database query.

The code is as follows:

<?php

$files=mysql_query("SELECT id,name,size,description,DATE_FORMAT(date,'%e %b') AS date FROM data WHERE site=".$site['id']." ORDER BY data.date DESC");
if(mysql_num_rows($files)==0)
echo " <tr><td>There is currently no data for this site.</td></tr>\n ";
else
while($file=mysql_fetch_array($files))
{
echo " <tr><td><a href=\"\" onclick=\"return showData();\">{$file['name']}</a> (".fsize($file['size']).", ".fdate($file['date']).")";
if(!empty($file['description'])) echo " - <span style=\"color:#606060;\">{$file['description']}</span>";
echo "</td></tr>\n ";
}
?>

What I want is for a "Delete" link to be provided next to each result of the dataset, i.e at the end of each document name. Clicking Delete next to the document name will delete that document but ONLY that document.

I had tried creating a new variable to do this:
$deletefile = "DELETE FROM data WHERE name = $files['name']";

But not sure how that would work- also looked into using Unlink but again not sure how I could use this specifically "per document" / "per file".

Any ideas?

mattclayb

11:42 am on Jul 20, 2009 (gmt 0)

10+ Year Member



I am assuming that you want to delete a physical document from the local server, as well as a database entry referring to the document?

There are a couple of subjects you may also want to look at when handling files being, -permissions and -owners. As the files must have the correct ownership and permissions for your scripts to safely delete them.

Also, as you are wanting the delete action through a 'url' and therefor using 'GET' varibales I would store and pass a 'random' reference for the file (rather than an auto int 'id'), so users cant hack the delete action easily and delete files. So store a random reference in the database for every file, and replace the $file['id'] field reference in the 'delete' url below with the reference for your random field.

That aside, something like this should work -

<?php

//Full document root file path (must be local to script)
//this is an example replace with your doc root
$filePath = "/root/home/httpdocs/files/";

$action = $_GET['action'];
if($action=="delete"){

$fID = $_GET['fID'];
$sql="SELECT id, name FROM data WHERE id=$fID LIMIT 1";
$query = mysql_query($sql);

if(@mysql_num_rows($query)>1){

$deleteFile = mysql_fetch_array($sql);

$sql="DELETE FROM data WHERE id=$fID LIMIT 1";
if(mysql_query($sql)){

unlink($filePath.$deleteFile['name']);
echo "<p style='color:red;'>The file $deleteFile['name'] has been deleted</p>";
}

}

}

$files=mysql_query("SELECT id,name,size,description,DATE_FORMAT(date,'%e %b') AS date FROM data WHERE site=".$site['id']." ORDER BY data.date DESC");
if(mysql_num_rows($files)==0)
echo " <tr><td>There is currently no data for this site.</td></tr>\n ";
else
while($file=mysql_fetch_array($files))
{
echo " <tr><td><a href=\"\" onclick=\"return showData();\">{$file['name']}</a> (".fsize($file['size']).", ".fdate($file['date']).")";
if(!empty($file['description'])) echo " - <span style=\"color:#606060;\">{$file['description']}</span>";

echo "<a href=\"?action=delete&fID=$file['id']\">Delete</a>";

echo "</td></tr>\n ";

}
?>

mattclayb

11:44 am on Jul 20, 2009 (gmt 0)

10+ Year Member



....Sorry...

this line - if(@mysql_num_rows($query)>1){

should read - if(@mysql_num_rows($query)>0){

mattclayb

11:45 am on Jul 20, 2009 (gmt 0)

10+ Year Member



$deleteFile = mysql_fetch_array($sql);

should be -

$deleteFile = mysql_fetch_array($query);

...sorry typing too fast !

galahad2

1:56 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



Unfortunately this generated this error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /hsphere/local/home/cellular/extranet.cellularasset.com/site/index.php on line 94

Line 94 being:

echo "<p>The file $deleteFile['name'] has been deleted</p>";

Any ideas?

mattclayb

2:48 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



try..

echo "<p>The file ".$deleteFile['name']." has been deleted</p>";

galahad2

3:19 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



Now getting this error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

on line 109

Line 109 is:

echo "<a href=\"?action=delete&fID=$file['id']\">Delete</a>";

mattclayb

3:33 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



same thing -

echo "<a href=\"?action=delete&fID=".$file['id']."\">Delete</a>";

galahad2

3:56 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



Okay, this time it doesn't error at all, but clicking the Delete link does nothing at all. (Although it seems to be picking up the ID of each of the records, which I can see if I hover over the various Delete links for each record)

?

galahad2

10:48 am on Jul 21, 2009 (gmt 0)

10+ Year Member



Anyone got any more ideas?

mattclayb

12:54 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



- is the field 'id' in your MySQl an int?
- is the field 'name' the actual filename of the file you want to delete?