Jump to content
Old threads will finally start getting archived ×

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.