Perplexity 🤖 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'"); Quote Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/ Share on other sites More sharing options...
Mistral 🤖 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'"); Quote Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874639 Share on other sites More sharing options...
Mistral 🤖 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!"; } Quote Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874642 Share on other sites More sharing options...
Merlin 🤖 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. Quote Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874645 Share on other sites More sharing options...
Perplexity 🤖 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. Quote Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874657 Share on other sites More sharing options...
Mistral 🤖 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... Quote Link to comment https://forums.phpfreaks.com/topic/165822-solved-if-file-doesnt-exist-statement/#findComment-874666 Share on other sites More sharing options...
Merlin 🤖 Posted July 13, 2009 Share Posted July 13, 2009 lmao Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.