So, I've been trying to come up with a PHP way to delete a file after deletion of the SQL record has been confirmed, but it occured to me, would it be easier to simply wipe the contents of the file? In other words, the file would still technically exist, but be null of any discernable content?
I'm looking at using a bit of code that I used in the file upload portion of the site, where a stray "w" deletes the file contents, I discovered.
Here's the code I'm currently using to delete the record:
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
if ((isset($_GET['FileID'])) && ($_GET['FileID'] != "")) {
$deleteSQL = sprintf("DELETE FROM SGD_FT WHERE FileID=%s",
GetSQLValueString($_GET['FileID'], "int"));
mysql_select_db($database_SGD_Database, $SGD_Database);
$Result1 = mysql_query($deleteSQL, $SGD_Database) or die(mysql_error());
$deleteGoTo = "SDir/dir.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
mysql_select_db($database_SGD_Database, $SGD_Database);
$query_DeleteFile = "SELECT * FROM SGD_FT";
$DeleteFile = mysql_query($query_DeleteFile, $SGD_Database) or die(mysql_error());
$row_DeleteFile = mysql_fetch_assoc($DeleteFile);
$totalRows_DeleteFile = mysql_num_rows($DeleteFile);
?>
This is what I thought initially about using to delete the file, but I'm not 100% sure how to make it work, so if this is the better route, please help in that regard:
function rfr($path,$match){
static $deld = 0, $dsize = 0;
$dirs = glob($path."*");
$files = glob($path.$match);
foreach($files as $file){
if(is_file($file)){
$dsize += filesize($file);
unlink($file);
$deld++;
}
}
foreach($dirs as $dir){
if(is_dir($dir)){
$dir = basename($dir) . "/";
rfr($path.$dir,$match);
}
}
return "$deld files deleted with a total size of $dsize bytes";
}
Now, this is the code I'm thinking of using to wipe the contents of the file:
$fp = fopen($upfile, 'r');
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);
Any and all suggestions are welcomed and hoped for!