繁体   English   中英

使用网址在网页中搜索HTML标记

[英]Search for a HTML tag in webpage using a url

我一直在一个项目中,我必须在用户给定的url输入中获取所有图像,并将用户找到并选择的所有图像的副本保存到他的帐户中。 我只是在这里寻找更好的解决方案。 除了保存从iframe中打开的当前页面中的图像外,我什至无法尝试其他任何操作。 请提出解决方案。

抱歉,我在问题中看到了*将用户找到并选择的所有图像的副本保存到他的帐户中,因此,我看到您只想保存用户选择的图像,我发布的内容不正确,下面有更多代码。

<?php
$allowed_formats  = array("png", "jpg", "gif");//Allowed image formats to save image as
$users_directory = "image-directory";//directory to save the images when user selects image to save

if ( isset( $_POST['view'] ) ) {//If url form submitted 
$users_submitted_url = $_POST['url'];//users submitted url
$ch = curl_init();//Curl the page
curl_setopt($ch, CURLOPT_URL , $users_submitted_url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$web_images_response = curl_exec ($ch);
curl_close($ch);

preg_match_all('@<img src="http://(.*?)"@', $web_images_response, $imgs);//match only full image links
$image_divs .= '';
foreach($imgs['1'] as $image_url){ 
$image_divs .= "<div><a href='".$PHP_SELF."?url=".$image_url."' target='_blank'><img height='200px' height='200px' src='http://".$image_url."'></a></div>";//make images if clicked use get method to save the image
}


}



if ($_GET["url"] == ""){//If url to save is blank do nothing else save image
}
else{
$split_image = pathinfo($_GET["url"]);
if (in_array($split_image['extension'], $allowed_formats)) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , "http://".$_GET["url"]);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$response= curl_exec ($ch);
curl_close($ch);
$file_name = $users_directory."/".$split_image['filename'].".".$split_image['extension'];//save image as file name and extension
$file = fopen($file_name , 'w') or die("X_x");
fwrite($file, $response);
fclose($file);
}
}
?>


<form method="post">
URL:<input name="url" value="http://piic.us/gallery.php" onclick="this.value='';"/>
<input name="view" value="Submit Query" type="submit" />
</form>

<?php echo $image_divs;?>

如果用户单击保存到该目录的图像,则希望这次我正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM