php - unlink not working when getting a url from loadHTML

746

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 throughloadHTML and then using$doc->getElementsByTagName('img') to get theattrsrc of the image. Then change the path of that image to the document root and then simplyunlink($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 usedtrim to make sure there is no white space ,but in this script is does not work

Now if I take the sameunlink('/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 aDOMDocument?

403

Answer

Solution:

When I run into stuff like this, I always usevar_dump() to see for sure what's in the variable. Sometimes things get missed with justecho().

Looks like in this case, it was an extra set of quotes. Glad you found it!

People are also looking for solutions to the problem: exception - Error handling inside PHP generator function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.