Forum Moderators: coopster
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?
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 ";
}
?>
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?