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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作