Is there a way to delete a file from the server using php? I have a database that stores listings for a real estate agent and if he deletes a listing, I would like to delete any images from the server associated with that listing as well.

Thanks in advance.

    Thanks Kudose. I have been trying the following and it doesn't seem to be deleting the files. It is however deleting the records from the database as it should though. What am I doing wrong?

    $sql2 = "Select * From photos Where listID = '".$id."' And listType = '".$cat."'";
    	$query = mysql_query($sql2);
    
    $mypath = "".$cat."Listings/images";
    while($remove = mysql_fetch_array($query)){
    	if($remove){
    		if(is_file($remove['fileName'])){
    			$deleteFile = "".$mypath."/".$remove['fileName']."";
    			unlink($deleteFile);
    		} 
    	}	
    } 
    
    $sql3 = "Delete From photos Where listID = '".$id."' And listType = '".$cat."'";
    $return = mysql_query($sql3);
    

    Thanks

      You need to use the absolute file path.

      i.e.

      /home/username/public_html/stuff/image.jpg

        Or at least use the same path in both is_file() and unlink().

          Write a Reply...