emediastudios Posted November 8, 2007 Share Posted November 8, 2007 Hi, have a delete record code that does the job and deletes the record, im trying to unlink the images that are related to that record. i added a code but it doesnt work. Here is my delete record code <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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($_POST['id'])) && ($_POST['id'] != "")) { $deleteSQL = sprintf("DELETE FROM news WHERE id=%s", GetSQLValueString($_POST['id'], "int")); mysql_select_db($database_p2w, $p2w); $Result1 = mysql_query($deleteSQL, $p2w) or die(mysql_error()); $deleteGoTo = "news_deleted.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; $myFile = "../images/news/". $_POST['photo1']; if (!empty($_POST['photo1'])) { unlink($myFile); } } header("Location: $deleteGoTo"); } mysql_select_db($database_p2w, $p2w); $query_Recordset1 = "SELECT * FROM news ORDER BY id ASC"; $Recordset1 = mysql_query($query_Recordset1, $p2w) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?> My photos are included in my form as <?php <input name="photo1" type="hidden" id="photo1" value="<?php echo $row_Recordset1['photo1']; ?>" /> <input name="photo2" type="hidden" id="photo2" value="<?php echo $row_Recordset1['photo1']; ?>" /> <input name="photo3" type="hidden" id="photo3" value="<?php echo $row_Recordset1['photo1']; ?>" /> ?> Once i get one file to be unlinked i ll work on an array to include the 3 files. My error is as follows Warning: unlink(../images/news/11.jpg) [function.unlink]: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php on line 97 Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php:97) in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\delete_news.php on line 103 Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/ Share on other sites More sharing options...
teng84 Posted November 8, 2007 Share Posted November 8, 2007 check if your directory is correct and check first if the file exist if (file_exists($myFile)){ unlink($myFile); } else{ //file doesnt exist } Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387890 Share on other sites More sharing options...
emediastudios Posted November 9, 2007 Author Share Posted November 9, 2007 Awesome, got rid of photo1, thanks heaps, How would i include photo2 and photo3 in this code? Again thank you Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387901 Share on other sites More sharing options...
teng84 Posted November 9, 2007 Share Posted November 9, 2007 loop $array =array('photo1','photo2','photo3'); foreach($array as $val){ if (file_exists($_POST[$val])){ unlink($myFile); } else{ //file doesnt exist } } Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387903 Share on other sites More sharing options...
emediastudios Posted November 9, 2007 Author Share Posted November 9, 2007 Sorry for sounding stupid but how do i make $myFile = "../news/". $_POST['photo1']; include the other 2 photos Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387913 Share on other sites More sharing options...
teng84 Posted November 9, 2007 Share Posted November 9, 2007 sorry $array =array('photo1','photo2','photo3'); foreach($array as $val){ $myFile = "../news/". $_POST[$val]; if (file_exists($myFile)){ unlink($myFile); } else{ //file doesnt exist } } edited... Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387916 Share on other sites More sharing options...
emediastudios Posted November 9, 2007 Author Share Posted November 9, 2007 Used this code //or $dir = "../news/" $array =array('photo1','photo2','photo3'); foreach($array as $val){ $myFile = "../news/". $_POST[$val]; if (file_exists($_POST[$val])){ unlink($myFile); //or unlink($dir.$_POST[$val]); } else{ //file doesnt exist } } and it deletes the record but no unlinking? Were close i know Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387929 Share on other sites More sharing options...
emediastudios Posted November 9, 2007 Author Share Posted November 9, 2007 Sorry Changed the code to this $array =array('photo1','photo2','photo3'); foreach($array as $val){ $myFile = "../news/". $_POST[$val]; if (file_exists($myFile)){ unlink($myFile); } else{ //file doesnt exist } It still only unlinks photo1 only Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387930 Share on other sites More sharing options...
teng84 Posted November 9, 2007 Share Posted November 9, 2007 $array =array('photo1','photo2','photo3'); foreach($array as $val){ $myFile = "../news/". $_POST[$val]; if (file_exists($myFile)){ echo $_POST[$val]; unlink($myFile); } else{ echo ++x; } i add echo to see if you really have those post data print the result here Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387933 Share on other sites More sharing options...
emediastudios Posted November 9, 2007 Author Share Posted November 9, 2007 found the problem, your the man, thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/76592-solved-cannot-unlink-file/#findComment-387965 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.