我正在尝试将此问题的输出存储在mysql中
filter images from webpage based on size
<?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中。
我试着填写表单字段中的这些echo详细信息,并且我的surprize只有第一行填写了表单字段以及所以无论mysql存储是按照表单字段输出我只是试图检查它的mysql问题或者是什么。我直接在phpmyadmin中执行了查询,所有内容都被存储但是通过表单它没有得到但是echo提供了完整的细节。
答案 0 :(得分: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
由于文件与条件不符,因此不会添加它们。这次只有两个图像添加到数据库中。