Check if image file exists in another server - php

I work on two servers (A and B, and I need to show on A some images from B. The problem is I need to test if the image exists (else I show only the name of the client).
When I test in local, this works:
if (file_exists(http://www.example.com/images/3.jpg)) {
// I show the file here
} else {
// I just show the name
}
But, that's don't works now. Help please !

You can't use file_exists for a checking remote files.
Use get_headers function and check, if the file returns 200 or not.
$file_headers = get_headers('http://www.example.com/images/3.jpg');
if ($file_headers[0] == 'HTTP/1.1 200 OK') {
echo "The file exists";
} else {
echo "The file doesn't exist";
}

try to use this
$array= get_headers("http://images.visitcanberra.com.au/images/canberra_hero_ima.jpg");
echo $h= $array[0];
it will return HTTP/1.1 404 Not Found if not exists and HTTP/1.1 200 OK if exists

function is_file_on_url_exists($url)
{
if (#file_get_contents($url, 0, NULL, 0, 1))
{
return true;
}
return false;
}
try this one out !!

Related

I'm always getting 301 HTTP header code even if the site doesn't exist (PHP)

I'm trying to figure out if a Instagram User exists or not. If the user site doesn't exist, it should give me a '404 Not Found' in the headers, but i'm always getting a '301 Moved Permanently', even if the user doesn't exist!
My browser gives me a 404:
https://i.imgur.com/to38Sb2.png
Can u help me? This is my code:
<?php
$url = "https://www.instagram.com/asdsdfsvxd"; //This user doesn't exist
echo $url;
$file_headers = #get_headers($url);
echo $file_headers[0];
if($file_headers[0] !== 'HTTP/1.1 404 Not Found') {
echo "exists";
} else {
echo "not exists";
}
?>
Solution:
Dont forget the trailing / (slash).
$url = "https://www.instagram.com/asdsdfsvxd/";
Output:
$ php test.php
https://www.instagram.com/asdsdfsvxd/HTTP/1.1 404 Not Foundnot exists

file_exists($img) and #getimagesize($img) are always returning false

I have a script that lets users upload an own header image for specific pages of my website. To check if the user has uploaded an image I use file_exists($img). But the function always returns false.
I've also tried using #getimagesize($img) which didn't work either. When I use echo "<img src='".$img."'> though, the picture shows without problem.
Maybe there's a mistake in my code, I don't know. Or maybe it has something to do with security issues? I tried using different files and chmod() though. Please help.
Here is my code:
define('PATH', '/root/');
define('LAYOUT', 'header1');
//This is always returning false
if (defined('LAYOUT') && (file_exists(PATH.'img/header/'.LAYOUT.'.png') OR
file_exists(PATH.'img/header/'.LAYOUT.'.jpg'))) {
echo "true";
} else {
echo "false";
}
//This is also returning false
if (defined('LAYOUT') && (#getimagesize($img)) {
echo "true";
} else {
echo "false";
}
//This is showing the picture
echo "<img style='width: 100%;' src='".PATH."img/header/".LAYOUT.".png'>";
EDIT: I solved it by using file_exists(__DIR__ . '\\www\\img\\header\\'.LAYOUT.'.png'). Thank you!

PHP Check if GET parameter is in .txt file and return a response yes/no

I'm new to PHP so I wanted to know how do I make a script which you have to put a GET parameter (index.php?test=value) and it checks is value is in a txt file provided. If the value exists then print yes if no then print no, if there isn't a GET parameter print like ''empty''. Please help me how to do this.
$_GET['test'] = 'value';
if(empty($_GET['test'])) {
echo 'empty';
} else {
$file ='asdfasdfasvalue';
if(strpos($file, $_GET['test'])) {
echo 'yes';
} else {
echo 'no';
}
}
You should populate $file with the file's contents.
Functions being used...
http://php.net/manual/en/function.empty.php
http://php.net/strpos
http://php.net/manual/en/function.file-get-contents.php

Reading file contents of a website?

All I want to check is that if a image exists in the link. I load it into an iframe. It was working fine but it seems they have removed the image but a blank.gif still exits.
NOTE: The link is a different domain
I tried the following codes in vain:
<?php
$varia = file_get_contents($url);
echo $varia;
echo "<pre>";
print_r(get_headers($url));
?>
and
$variablee = get_data($url);
pr($variablee);
All I get in the output is:
HTTP ERROR 404
Problem accessing
I want to put the condition that if blank.gif exits......some condition else some other condition.
What should I do?
In my opinion, the best way to test if an URI actual is an image, is to see what getimagesize returns. If it returns an array, it is an image :
function imageExists($uri) {
$info = #getimagesize($uri);
return is_array($info);
}
if (imageExists($uri)) {
...
}
Try
$content = #file_get_contents($url);
if ($content !== false) {
// FILE EXISTS
} else {
// FILE NOT EXISTS
}
?>
You can use this code:
if(getimagesize($url))
{
//image exist
}
else
{
//image not exist
}

Check if YouTube User Exists with PHP

Okay so I'm trying to redirect a user back to subto.me.com if subto.me/args doesn't exist I have this code
if(file_exists($BASEPATH."partials/".$request['args'][0].".php")) {
require($BASEPATH."partials/".$request['args'][0].".php");
} else {
header("Location: http://www.youtube.com/subscription_center?add_user=".$request['args'][0]);
}
If the youtube user doesn't exist, it will say so using a link like this https://gdata.youtube.com/feeds/api/users/fdsafsdfasdfasd. How would I incooperate it if source code equals "User not found"
I have a solution for that:
$yuser = "http://www.youtube.com/user/xxxx";
$yt_headers = #get_headers($yuser);
if($yt_headers[0] == 'HTTP/1.1 404 Not Found')
{ echo "Youtube user dosen't exist"; }
else { echo "Youtube user exists"; }
this can be used as a function to make the process more pretty!

Categories