dongshang1768
2011-12-02 03:52
浏览 68
已采纳

将图像元素保存在特定div中

<div class='film' id='1'>
<img src='abc.jpg'></img>
<p>blablabla</p>
</div>
<div class='film' id='2'>
<img src='dfdf.jpg'></img>
<p>blablabla</p>
</div>
<div class='film' id='3'>
<img src='fgsf.jpg'></img>
<p>blablabla</p>
</div>
<div class='film' id='4'>
<img src='dfg.jpg'></img>
<p>blablabla</p>
</div>
.
.
.

I create 5700 divs with class 'film' with a PHP file. Div ids are counting. Each div contains a '<p>' element and '<img>' element. Paragraph and image elements doesn't have a class or id.

I want to save image elements in these divs in a folder named 'filmimages' . And I need to insert URLs into my database.

My code should do this.

div with the id 1 -> save the image element to the folder -> filmimages/abc.jpg -> insert into table (url) where id='1' values 'filmimages/abc.jpg'

How to I do it with php?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglei7152 2011-12-02 04:05
    已采纳

    Well you're going to want to load that HTML into DOM and use getElementsByTagName to get an array of those images. Then you are going to want to use file_put_contents to save it. so...

    $dom=new DOMDocument();
    $dom->loadHTMLFile($file);
    $images=$dom->getElementsByTagName('img');
    foreach($images as $image) {
        file_put_contents('filmimages/abc.jpg', file_get_contents(strval($image['src'])));
    }
    

    Or something like that. I haven't tested this code, but it seems right. As for your database, good luck, I've only ever messed with XML not any SQL yet :)

    点赞 评论

相关推荐 更多相似问题