0

我正在尝试将这个问题的输出存储在 mysql 中

根据大小过滤网页中的图像

<?php 

include('connect.php'); 

$html = file_get_contents("http://santabanta.com/photos/amisha-patel/402186.htm"); 

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName('img'); 

foreach ($tags as $tag) { 
    $data = get_headers($tag->getAttribute('src'),1); 
    if((intval($data["Content-Length"])/1024)>=10){ 
        echo $tag->getAttribute('src');
        $url=''.$tag->getAttribute('src').'';
        echo $url;
        mysql_query ("INSERT INTO table1 (url) VALUES ('" . mysql_real_escape_string($url) . "')");
    }
} 

?>

但令我惊讶的是,只存储了输出的第一个链接/链接。我已经使用 echo 进行检查,并且 echo 给出了正确的输出。

我用于存储此代码的 mysql 数据类型是文本,我正在使用此 mysql 查询插入 mysql 但只保存第一行。

mysql_query ("INSERT INTO tablea (url) VALUES ('" . mysql_real_escape_string($url) . "')");

从 echo 获取结果时一切似乎都很好,但后来它没有存储在 mysql 中。

我试图在表单字段中填写这些回显详细信息,令我惊讶的是,只有第一行也填写了表单字段,所以无论 mysql 存储什么都是按照表单字段输出,我只是试图检查它的 mysql 问题还是什么。我直接在 phpmyadmin 中执行了查询,所有内容都被存储了,但是通过表单它没有得到但是 echo 给出了完整的细节。

4

1 回答 1

1

这是我使用的代码

<?php 

include('connect.php'); 

$html = file_get_contents("http://santabanta.com/photos/amisha-patel/402186.htm"); 

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName('img'); 

foreach ($tags as $tag) { 
    $data = get_headers($tag->getAttribute('src'),1); 
    $filesize = (intval($data["Content-Length"])/1024);
    // Modify the integer below to what size KB you want.
    if($filesize >= 10){ 
        //echo $tag->getAttribute('src');
        $url = $tag->getAttribute('src');
        // Do the insert here so it enters if all criteria are met.
        $query = "INSERT INTO tablea (url) VALUES ('" . mysql_real_escape_string($url) . "')";
        $result = mysql_query($query);
        // This to check what the error may be and your insert statement.
        if(!$result) echo mysql_error() . " | " . $query . "<br />";
    } else {
        echo "File not added: ".$tag->getAttribute('src')." | " .$filesize. "kb<br />";
    }
}

?>

我的结果如下:

File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/sambhavna%20seth/sambhavna-seth-10a.jpg | 6.015625kb
File not added: http://media.santabanta.com/medium/bollywood%20movies/jism%202/jism-2-17a.jpg | 4.9638671875kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/kangana%20ranaut/kangana-ranaut-45a.jpg | 5.09375kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/bidita%20bag/bidita-bag-0a.jpg | 8.7138671875kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/ileana/ileana-14a.jpg | 6.703125kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/yami%20gautam/yami-gautam-6a.jpg | 4.78515625kb
File not added: http://media.santabanta.com/medium/bollywood%20movies/ek%20tha%20tiger/ek-tha-tiger-15v.jpg | 6.15234375kb
File not added: http://media.santabanta.com/medium/events/independence%20day/independence-day-78a.jpg | 8.470703125kb
File not added: http://media.santabanta.com/medium/bollywood%20movies/ek%20tha%20tiger/ek-tha-tiger-14a.jpg | 5.8408203125kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/diana%20penty/diana-penty-3a.jpg | 7.7099609375kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/sana%20khan/sana-khan-2a.jpg | 6.6640625kb
File not added: http://media.santabanta.com/medium/emotions/love/love-126a.jpg | 4.18359375kb
File not added: http://b.scorecardresearch.com/p?c1=2&c2=13655906&cv=2.0&cj=1 | 0.0009765625kb

由于文件不符合条件,因此不会添加它们。这次只有两个图像添加到数据库中。

于 2012-08-17T02:23:41.480 回答