title: php delete file using unlink before database row update
body: <p>I'm setting up a CRUD and realized that I will need to delete the image that's stored in a directory if the user wants to upload a new image.</p>
  
  <p>I have a webpage with a form that brings the information from the database row using the id, then posts the updated values to a script which is where the trouble is.</p>
  
  <p>I'm trying to find the file that needs to be deleted with this:</p>
  
  <pre><code>$target_dir = "images/photo/";
  $del_image = $_FILES["image"];
  </code></pre>
  
  <p>And trying to set the permissions of the file with this:</p>
  
  <pre><code>$change = chmod($del_image,0644);
  </code></pre>
  
  <p>Then trying the delete the file with this:</p>
  
  <pre><code>$delete = unlink($target_dir.$image);
  </code></pre>
  
  <p>Before I update everything with this:</p>
  
  <pre><code>$target_file = $target_dir . basename($_FILES["image"]["name"]);
  $file = $target_dir . basename($_FILES["ud_image"]["name"]);
  $uploadOk = 1;
  if (move_uploaded_file($_FILES["ud_image"]["tmp_name"], $file))
  {
  echo '&lt;script type="text/javascript"&gt;';
  echo 'alert("News Items Saved")';
  echo '&lt;/script&gt;';
  } else {
      echo "Sorry, there was an error with your file.";
  }
  $id = intval($_GET['id']);
  $ud_headline = $_POST["ud_headline"]; //mysql_real_escape_string()
  $ud_body = $_POST["ud_body"]; //mysql_real_escape_string()
  $ud_image = $_POST["ud_image"]; //mysql_real_escape_string()
  
  
  $query="UPDATE news SET 
      headline = '$ud_headline',
      body = '$ud_body',
      image = '$ud_image' 
      WHERE id='$ud_id'";
  
  
  $mysqli-&gt;query($query)or die($mysqli-&gt;error);
  if($mysqli-&gt;affected_rows&gt;=1){
  echo "&lt;script type='text/javascript'&gt;";
  echo "alert('News Item Updated');";
  echo 'document.location.href = "/pc.v.2/admin-news.php";';
  echo "&lt;/script&gt;;";
  }
  else
  {
  echo "&lt;script type='text/javascript'&gt;";
  echo "alert('News Item Not Updated'. $mysqli-&gt;error);";
  echo "&lt;/script&gt;";
  //echo "Error deleting record: " . $conn-&gt;error;
  }
  </code></pre>
  
  <p>The errors I get are telling me that I'm not even finding the directory correctly, let alone the file.</p>
  
  <p>This is the form:</p>
  
  <pre><code>&lt;form action="update.php" method="post" class="newNews"&gt;
  &lt;input type="hidden" name="ud_id" value="&lt;?=$id;?&gt;"&gt;
  &lt;!-- &lt;input type="hidden" name="old_id" value="&lt;?=$image;?&gt;"&gt; --&gt;
  
  &lt;label for="title"&gt;Title&lt;/label&gt;
  &lt;input type="text" name="ud_headline" value="&lt;?=$headline;?&gt;"/&gt;&lt;br /&gt;
  
  &lt;label for="text"&gt;Body&lt;/label&gt;
  &lt;textarea name="ud_body" rows="15" cols="21" value=""&gt;&lt;?=$body;?&gt;&lt;/textarea&gt;&lt;br /&gt;
  
  &lt;p&gt;Current Photo&lt;/p&gt;
  &lt;img src="&lt;?=$target_dir.$image?&gt;" alt=''&gt;&lt;br /&gt;
  
  &lt;input type="file" name="ud_image" class="newsImage" &gt;&lt;br /&gt;
  
  &lt;input type="submit" name="submit" value="Update news item" class='addNew' /&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>How can I fix this?</p>