I have a weird issue. I have a script that I run which will loop through a table that has some records in it containing HTML. In that html are images that I need to delete when the script is ran. Here is how I am doing it
while ($row_find = $result_find->fetch_array()) {
$comment = $row_find['comment']; <--this is html
$html = $comment;
$doc = new DOMDocument();
@$doc->loadHTML($html);
$imgs = $doc->getElementsByTagName('img');
// images
foreach ($imgs as $img) {
$src = stripslashes(trim($img->getAttribute('src')));
$img_new = str_replace("https://www.example.com","/home/document/path/html/root",$src);
unlink($img_new);
}
}
What I am doing is loading the html through loadHTML
and then using $doc->getElementsByTagName('img')
to get the attr
src
of the image. Then change the path of that image to the document root and then simply unlink($img_new)
Problem is this is not working. I have checked the path a million times and it is correct, the permissions on the folder and file are correct. I have used trim
to make sure there is no white space ,but in this script is does not work
Now if I take the same unlink('/home/document/path/html/root/thisimage.png')
except I write in the path manually and place it at the top of the script it works fine and the image is removed.
The path is correct, but it will not delete. Is this something to do with creating the image path from a DOMDocument
?
When I run into stuff like this, I always use var_dump()
to see for sure what's in the variable. Sometimes things get missed with just echo()
.
Looks like in this case, it was an extra set of quotes. Glad you found it!