I found this nifty way to check if a page really exists:
$headers=#get_headers($imageurl);
if(strpos($headers[0],'200')===false){
echo "not valid";
exit;
}
What I need to achieve, though, is to check whether on that page is really only an image.
For example, when you try the following link:
www.google.com/image.jpg
it will return 200 because Google has it's own error page - however, it should be possible to find out not only that there is no image on that page, but also that an image is not the only thing on that page, even when there is other content (like here: http://www.kapstadt-entdecken.de/wp-content/gallery/robben-island/1robben-island.jpg).
How I can I achieve that?
Thanks a lot!
Dennis
You will probably want to use HEAD requests when getting headers (it doesn't do that by default):
stream_context_set_default(array(
'http' => array(
'method' => 'HEAD'
)
));
Second, you can pass a second parameter to get_headers to parse it for you:
$headers = get_headers($imageurl, 1);
Then, you can check the rest as per normal:
if (strpos($headers[0], '200') === false) {
echo "not valid";
exit;
}
if (isset($headers['Content-Type']) && 0 === strncmp($headers['Content-Type'], 'image/', 6)) {
echo "valid image";
} else {
echo "probably not an image";
}
also with get_headers ... one of them will be
image/png
image/jpeg
image/gif
so
$isImage = false;
foreach($headers as $header){
if(strpos($header,'image/')==true){
$isImage = true;
}
}
$headers = #get_headers($imageurl);
$is_image = false;
foreach ($headers as $header) {
if (strpos($header, 'Content-Type: image/') === 0) {
$is_image = true;
break;
}
}
Related
i try to check if image exists and when i execute code say "Not Image".
if(#is_array(getimagesize("https://media.giphy.com/media/BvvBz8BnRqZOg/giphy.gif"))) {
echo "Work";
} else {
echo "Not Image";
}
Where is problem, Thanks in advance !
Try
$headers = get_headers('https://media.giphy.com/media/BvvBz8BnRqZOg/giphy.gif', 1);
if (strpos($headers['Content-Type'], 'image/') !== false) {
echo "Work";
} else {
echo "Not Image";
}
You only need to check the headers to see it it is an image, $headers['Content-Type'] in the example is 'image/gif' which is caught by the if statement. For reference check out get headers in the docs http://php.net/manual/en/function.getallheaders.php
ive a database with around 5000 videos and i noticed some of them are removed now.. SO i decided to write a php script to fix bulk check this..
From the various sources below is the code i implemented based on most answers here, but it doesnt give correct results. IT gives a 403 header for 3/4th videos though practically more than 90% are working..Am i missing anything?
foreach ($video as $cat) {
$str = explode("=",$cat->videourl);
$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $str[1]);
if (!strpos($headers[0], '200')) {
print_r($headers[0].'<br>');
$i=$i+1;
print_r("Unpublish".$cat->id. PHP_EOL);
}
else{
print_r("publish".$cat->id. PHP_EOL);
}
}
I'm printing the header here to debug it, and for most it gives, HTTP/1.0 403 Forbidden
Edit :: ive already checked the videoids are passed correctly(so string processing has no issues)
For anyone trying to achieve this, here is the code.. do appreciate if works for you as ive spend hours to get it working for the new api
$headers = checkYoutubeId ($str[1]);
if ($headers == false) {
$i=$i+1;
$db->query('UPDATE `ckf_hdflv_upload` SET `published`="0" Where `id`='.$cat->id);
print_r("Unpublished".$cat->id. PHP_EOL);
}
else{
$db->query('UPDATE `ckf_hdflv_upload` SET `published`="1" Where `id`='.$cat->id);
}
}
}
echo('done'.$i);
function checkYoutubeId($id) {
if (!$data = #file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$id)) return false;
if ($data == "Video not found") return false;
if ($data == "Private video") return false;
return true;
}
am testing lots of links on the same domain to see whether they exist or not. I am using the following code:
function get_http_response_code($url)
{
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
function getURLs()
{
foreach($allResults as $result)
{
$tempURL = 'http://www.doma.in/foo/'.$result.'/bar';
if(get_http_response_code($tempURL) != "404" && get_http_response_code($tempURL) != "500")
{
$URLs[] = $tempURL;
}
else
{
echo $tempURL.' could not be reached<br />';
}
return $URLs;
}
$URLs = getURLs();
The problem is, among the hundreds that do exist, the $URLs array contains URLs that do not exist (404); sometimes two, sometimes four, but every time it produces an HTTP/1.0 404 Not Found error. Why such variance? Is there a timeout I should be setting? Any help will be appreciated.
As I understand from Your code the problem is in mistake of variable $url
Try this.
...
foreach($allResults as $result)
{
$tempURL = 'http://www.doma.in/foo/'.$result['url'].'/bar';
...
$url changed to $result
Here is my code, you can view an example of it by going to:
www.craftquake.com/statusChecker.php?site=MCnet
<?php
$getter = $_GET['site'];
if ($getter == 'ts3')
{ $site = test_port('ts3.craftquake.com',10011,4); }
if ($getter == 'MCquake')
{ $site = test_port('play.craftquake.com',25565,4); }
if ($getter == 'MCnet')
{ $site = test_port('minecraft.net',80,4); }
$teamspeak = test_port('ts3.craftquake.com',10011,4);
$online = '<img src="/online.png">';
$offline = '<img src="/offline.png">';
$unknown = '<span class="status-unknown" id="status-image">Unknown</span>';
function test_port($host,$port=80,$timeout=1)
{
$fsock = fsockopen($host, $port, $errno, $errstr, $timeout);
if ( ! $fsock )
{
return FALSE;
}
else
{
return TRUE;
}
}
?>
##HEADER & CSS, ETC
<?php
if ($site == 1)
{ $status = $online;
} else if ($site == 0) {
$status = $offline;
} else {
$status = $unknown;
}
header('content-type: image/png');
readfile($status);
echo $status;
?>
I want to, in the footer of my page, link to this page to display the status. I was doing this with another site's script by linking their status of Minecraft.net's servers as the and it worked perfectly, however I have no idea how they made that work. The images are PNG's, but if there is only one format that works, I can convert them.
I have tried the header(blablabla) function, but it doesn't seem to work...
Thank you very much!
Your variables contain HTML instead of the path name to the image files:
$online = '<img src="/online.png">';
should be:
$online = 'online.png';
Create a unknown status image and put it in $unknown too.
An image should be a seperate request (so, put an <img src="/yourimagescript.php"> in your html page, and in that seperate script output only the image, no html. You could embed (small) images with the data: protocol, but I strongly advise against it.
I have an input box that tells uers to enter a link from imgur.com
I want a script to check the link is for the specified site but I'm not sue how to do it?
The links are as follows: http://i.imgur.com/He9hD.jpg
Please note that after the /, the text may vary e.g. not be a jpg but the main domain is always http://i.imgur.com/.
Any help appreciated.
Thanks, Josh.(Novice)
Try parse_url()
try {
if (!preg_match('/^(https?|ftp)://', $_POST['url']) AND !substr_count($_POST['url'], '://')) {
// Handle URLs that do not have a scheme
$url = sprintf("%s://%s", 'http', $_POST['url']);
} else {
$url = $_POST['url'];
}
$input = parse_url($url);
if (!$input OR !isset($input['host'])) {
// Either the parsing has failed, or the URL was not absolute
throw new Exception("Invalid URL");
} elseif ($input['host'] != 'i.imgur.com') {
// The host does not match
throw new Exception("Invalid domain");
}
// Prepend URL with scheme, e.g. http://domain.tld
$host = sprintf("%s://%s", $input['scheme'], $input['host']);
} catch (Exception $e) {
// Handle error
}
substr($input, 0, strlen('http://i.imgur.com/')) === 'http://i.imgur.com/'
Check this, using stripos
if(stripos(trim($url), "http://i.imgur.com")===0){
// the link is from imgur.com
}
Try this:
<?php
if(preg_match('#^http\:\/\/i\.imgur.com\/#', $_POST['url']))
echo 'Valid img!';
else
echo 'Img not valid...';
?>
Where $_POST['url'] is the user input.
I haven't tested this code.
$url_input = $_POST['input_box_name'];
if ( strpos($url_input, 'http://i.imgur.com/') !== 0 )
...
Several ways of doing it.. Here's one:
if ('http://i.imgur.com/' == substr($link, 0, 19)) {
...
}