• PHP Help
  • Can't unlink files from directory...

I am trying to delete a few files in a loop. It is not working and just curious if someone could offer some oversight, thanks.

`
<?
$sqlz = "SELECT lessonid, imagelesson FROM steam_lessons WHERE lessonid = ".$idToDelete."";
$resultx = mysqli_query($con, $sqlz);

$root = str_replace('\', '/', $_SERVER['DOCUMENT_ROOT']);

while ($rowx = mysqli_fetch_array($resultx)) {
$delimage = unlink(''.$root.'/images/thumbs/'.$rowx[imagelesson].'');
$delimage .= unlink(''.$root.'/images/large/'.$rowx[imagelesson].'');
echo $delimage;
}

?>
`

    Well, I can see a number of problems. But one of the biggies is that $rowx[imagelesson] should be $rowx['imagelesson']. Another is that you have an unescaped backslash in your str_replace call.

    Aside from that you forget to say what it is doing instead.

      I'll post the conditional part of my code. It's still not unlinking the files.

      elseif(isset($_POST["recordToDelete"]) && strlen($_POST["recordToDelete"])>0 && is_numeric($_POST["recordToDelete"]))
      {
      
      //do we have a delete request? $_POST["recordToDelete"]
      
      //sanitize post value, PHP filter FILTER_SANITIZE_NUMBER_INT removes all characters except digits, plus and minus sign.
      $idToDelete = filter_var($_POST["recordToDelete"],FILTER_SANITIZE_NUMBER_INT);
      
      //try deleting record using the record ID we received from POST
      $delete_row = $mysqli->query("DELETE FROM db_lessons WHERE lessonid=".$idToDelete);
      
      //delete raw file
      
      $sqlz = "SELECT lessonid, imagelesson FROM db_lessons WHERE lessonid = ".$idToDelete."";
      $result = $mysqli->query($sqlz);
      
      $root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
      
      while ($rowx = $result->fetch_array(MYSQLI_ASSOC)) {
      unlink(''.$root.'/images/thumbs/'.$rowx['imagelesson'].'');
      unlink(''.$root.'/images/large/'.$rowx['imagelesson'].'');
      }
      
      if(!$delete_row)
      {
      	//If mysql delete query was unsuccessful, output error
      	header('HTTP/1.1 500 Could not delete record!');
      	exit();
      }
      
      $mysqli->close(); //close db connection
      
      
      }
      

        Wrapped the above code in the forum's [code]...[/code] tags. They work much better for blocks of code than the formatting that the </> option does for blocks of code.

          So you're deleting the record (record(s) multiple it looks like, based on the loop) that contains the file names you want to delete before before you get the file names you want to delete out of it/them?

            Write a Reply...