Jump to content

[SOLVED] If File Doesn't Exist Statement


mcmuney

Recommended Posts

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

$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.