mcmuney Posted July 13, 2009 Share Posted July 13, 2009 Below is a portion of the code that I'm using to remove a file and it's DB record. The DB record is deleted only if the file is deleted by this script; however, the issue is that it doesn't remove the DB record if the file is missing. I need line 2 modified where line 3 will execute in either or conditions: 1) if the file doesn't exist in path OR 2) if file was deleted by script. 1 $path = "photos/".$date."/".$id.".jpg"; 2 if(unlink($path)){; 3 $sql2 = mysql_query("DELETE FROM images WHERE id = '$id'"); Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/ Share on other sites More sharing options...
.josh Posted July 13, 2009 Share Posted July 13, 2009 just remove the condition altogether? 1 $path = "photos/".$date."/".$id.".jpg"; 2 unlink($path); 3 $sql2 = mysql_query("DELETE FROM images WHERE id = '$id'"); Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874639 Share on other sites More sharing options...
HERATHEIM Posted July 13, 2009 Share Posted July 13, 2009 $path = "photos/".$date."/".$id.".jpg"; if (file_exists($path)) { if(unlink($path)) { $sql2=mysql_query("DELETE FROM images WHERE id = '$id'"); } } else { echo "file is missing!"; } Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874642 Share on other sites More sharing options...
phporcaffeine Posted July 13, 2009 Share Posted July 13, 2009 $path = "photos/".$date."/".$id.".jpg"; @unlink($path); $sql2 = mysql_query("DELETE FROM images WHERE id = '$id'"); In the above example regardless if the file was present for unlink to delete, the query statement is ran. The '@' symbol simply squelches any error messages that you would get when unlink tried to remove a file that wasn't there. Not the most elegant, but the quickest. Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874645 Share on other sites More sharing options...
mcmuney Posted July 13, 2009 Author Share Posted July 13, 2009 Crayon Violent: Removing the condition altogether would not remove the file. HERATHEIM: I think I'd need an OR statement if (!file_exits) OR (unlink) etc. phpORcaffine: That did the trick Thanks. Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874657 Share on other sites More sharing options...
.josh Posted July 13, 2009 Share Posted July 13, 2009 Crayon Violent: Removing the condition altogether would not remove the file. Really? Because the only difference between what I posted and phpORcaffine posted is that he threw an @ in there to suppress potential warnings... Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874666 Share on other sites More sharing options...
seventheyejosh Posted July 13, 2009 Share Posted July 13, 2009 lmao Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.