簡體   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