Can I add a MySQL variable $row_FileUpdate['FileLoc'] to an unlink() function? I'm trying to delete a file on the server so where the location ("FileLoc" in the SQL db) is stored in SQL. I don't care if the file is actually deleted or the file contents are simply nullified; I just need something to save space on the server. Any suggestions?
Adding SQL variable to unlink()
AndrewH wrote:Can I add a MySQL variable $row_FileUpdate['FileLoc'] to an unlink() function?
Yes. Why? Because there is no such thing as a "MySQL variable." That variable (at least, one can assume) is simply a variable containing a string retrieved from a MySQL database.
This is the way my DB is setup:
Row: FileID File Name FileLoc(ation) FileUpdate User
Ex: 1 Test File FDir/testfile.txt 04-11-2007 AndrewH
What I want to happen is when I click "delete file" in the DB generated list of files on the server, I have it set to delete the record (in this case FileID=1) in the MySQL DB but I want it to also delete FDir/testfile.txt from the server. When the user clicks delete the first time, it come up with a confirmation that displays the file information to confirm that the correct file is about to be deleted. It is based on FileID, in this case "1." So, would I be correct to code "unlink('$row_FileUpdate['FileLoc']')"
Without the single quotes around the $row_FileUpdate variable, yes (assuming that the FileLoc value is a path relative to wherever this script is running).
Sorry, I normally can figure out PHP and SQL, but this one just confuses me.
So, this is the code I'm using:
<?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;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "UpdateRecord")) {
$updateSQL = sprintf("UPDATE SGD_FT SET FileName=%s WHERE FileID=%s",
GetSQLValueString($_POST['FileName'], "text"),
GetSQLValueString($_POST['FileID'], "int"));
mysql_select_db($database_SGD_Database, $SGD_Database);
$Result1 = mysql_query($updateSQL, $SGD_Database) or die(mysql_error());
$updateGoTo = "r3S.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
$colname_FileUpdate = "-1";
if (isset($_GET['FileID'])) {
$colname_FileUpdate = (get_magic_quotes_gpc()) ? $_GET['FileID'] : addslashes($_GET['FileID']);
}
mysql_select_db($database_SGD_Database, $SGD_Database);
$query_FileUpdate = sprintf("SELECT * FROM SGD_FT WHERE FileID = %s", $colname_FileUpdate);
$FileUpdate = mysql_query($query_FileUpdate, $SGD_Database) or die(mysql_error());
$row_FileUpdate = mysql_fetch_assoc($FileUpdate);
$totalRows_FileUpdate = mysql_num_rows($FileUpdate);
?>
Where do I put unlink($row_FileUpdate['FileLoc']) ???
The page previous to this displays the data from all rows where FileID=%s, so where FileID=%s should still pull a path relative to the page, but I need to add FDir/ in there somewhere as that's the directory that's the file is in but it's not in the data on the table.
AndrewH wrote:Where do I put unlink($row_FileUpdate['FileLoc']) ???
Obviously, after you define the $row_FileUpdate variable.
AndrewH wrote:I need to add FDir/ in there somewhere as that's the directory that's the file is in but it's not in the data on the table.
Okay... so do that. Instead of just passing the variable to the unlink() function, concatenate the string "FDir/" before the variable.