GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP Checking if URL/File Exists... Return True/False (https://gfy.com/showthread.php?t=926285)

Voodoo 09-07-2009 02:57 PM

PHP Checking if URL/File Exists... Return True/False
 
Code:

<?php if(file_exists("http://www.site.com/images/$id/misc6.jpg")){ DO THIS }?>
For some reason this doesn't seem to work. When I remove this check, the image shows up under { DO THIS }, however, the check doesn't seem to be returning a TRUE on this statement. What's the best way to return TRUE/FALSE for a remote image if it exists?

EDIT: Note... $id is assigned above this statement.

Voodoo 09-07-2009 03:09 PM

Bumpity Bump

Varius 09-07-2009 03:09 PM

file_exists doesn't support checks on remote files.

why not just try with fopen ?

Dirty D 09-07-2009 03:50 PM

also make sure

allow_url_fopen is enabled

(Dirty D has chicks in the pool on Labor Day, why am I commenting on someone else's code?)

mikeyddddd 09-07-2009 04:25 PM

Quote:

Originally Posted by Dirty D (Post 16289689)
(Dirty D has chicks in the pool on Labor Day, why am I commenting on someone else's code?)


calmlikeabomb 09-08-2009 04:31 AM

The most secure way to accomplish what you need is using cURL.

Many will argue that enabling URL wrappers is a potential security risk within PHP. Just google and you'll find many articles. This is why that option is turned off by default in the configuration file.

nation-x 09-08-2009 05:02 AM

if the file is an image every time... you can use getimagesize >> http://us2.php.net/getimagesize

Code:

Example #3 getimagesize (URL)
<?php
$size = getimagesize("http://www.example.com/gifs/logo.gif");

// if the file name has space in it, encode it properly
$size = getimagesize("http://www.example.com/gifs/lo%20go.gif");

?>


nation-x 09-08-2009 05:04 AM

Quote:

Originally Posted by calmlikeabomb (Post 16291116)
The most secure way to accomplish what you need is using cURL.

Many will argue that enabling URL wrappers is a potential security risk within PHP. Just google and you'll find many articles. This is why that option is turned off by default in the configuration file.

allow_url_fopen isn't really a big risk... allow_url_include is.

calmlikeabomb 09-08-2009 06:10 AM

allow_url_include doesn't work unless allow_url_fopen is enabled, buddy.

nation-x 09-08-2009 07:22 AM

Quote:

Originally Posted by calmlikeabomb (Post 16291312)
allow_url_include doesn't work unless allow_url_fopen is enabled, buddy.

They have separate flags... so it is possible to enable allow_url_fopen and turn off allow_url_include.

Davy 09-08-2009 09:09 AM

http://i259.photobucket.com/albums/h...er/curling.jpg

GrouchyAdmin 09-08-2009 09:15 AM

This is incredibly basic shit. The following is a top-down-no-classes-used example, using cURL, as suggested above. Note the lack of RETURNTRANSFER as we're only checking the status code.

Code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.site.com/blah/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch,CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$pageData=curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

switch ($statusCode) {
  case 404:
    filemissing()...
    break;
  case 401:
    authreq();
    break;
  default:
  whatev();
}

Quote:

Originally Posted by nation-x (Post 16291190)
if the file is an image every time... you can use getimagesize >> http://us2.php.net/getimagesize

Do not use getimagesize() as a boolean for validation - that is VERY unsafe.

nation-x 09-08-2009 01:28 PM

Quote:

Originally Posted by GrouchyAdmin (Post 16291900)
Do not use getimagesize() as a boolean for validation - that is VERY unsafe.

Please provide an example to prove your assertion... how exactly is this unsafe?

This is how I would use it.

Code:

$file_url = "http://www.site.com/images/{$id}/misc6.jpg";
$file_exists = is_array($file_url);


GrouchyAdmin 09-08-2009 01:32 PM

Quote:

Originally Posted by nation-x (Post 16293165)
Please provide an example to prove your assertion... how exactly is this unsafe?[/code]

Generally, lazily coded image hosting scripts. The easiest way to abuse this I've actually seen in the wild. It starts with a valid GIF header, and has a PHP script as the payload. WIthout giving away too many details, file naming conventions and autonegotiation can cause you bigtime issues.

nation-x 09-08-2009 01:38 PM

should have been:
Code:

$file_url = "http://www.site.com/images/{$id}/misc6.jpg";
$file_exists = is_array(getimagesize($file_url));


MichaelP 09-08-2009 01:50 PM

$yourfile = "full/path/to/your/file.php-jpg-gif-whatever" ;
IF (file_exists($yourfile)) {
print "Your File Does Existst" ; // TRUE
} ELSE {
print "Your file Doesn't exists" ; // FALSE
}

Tom_PM 09-08-2009 01:54 PM

I ran into this issue long ago, but I think I solved it by realising that both domains I was working with were on the same server, doh!

Good luck.

Killswitch - BANNED FOR LIFE 09-08-2009 02:15 PM

Quote:

Originally Posted by MichaelP (Post 16293268)
$yourfile = "full/path/to/your/file.php-jpg-gif-whatever" ;
IF (file_exists($yourfile)) {
print "Your File Does Existst" ; // TRUE
} ELSE {
print "Your file Doesn't exists" ; // FALSE
}

Not gonna work captain.

ProG 09-08-2009 02:16 PM

Quote:

Originally Posted by Killswitch (Post 16293411)
Not gonna work captain.

I don't even think he read the thread.

GrouchyAdmin 09-08-2009 02:32 PM

Jeeesus peoples. This is a hack of a slightly-better-than-my-above for just checking for the files' existence, based upon code at PHP.net; I figured this thread should die a horrible death, so here ya go.

Code:

function http_file_exists($url=FALSE) {
  // Bad URL, bail.
  if (!(@parse_url($url))) return FALSE;
  // No cURL, bail.
  if (!(function_exists('curl_init'))) return FALSE;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_NOBODY, 1);
  curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  return (curl_exec($ch) !== FALSE) ? TRUE : FALSE;
}

Here's even a (shitty) working example:

Code:

$urltest = array ("http://www.google.com/logo.gif", "http://www.google.com/intl/en_ALL/images/logo.gif");

foreach ($urltest as $url) {
  echo http_file_exists($url) ? "$url exists.\n" : "$url is a 404.\n";
}
exit;

Code:

http://www.google.com/logo.gif is a 404.
http://www.google.com/intl/en_ALL/images/logo.gif exists.

Can this go away now pls?

nation-x 09-08-2009 02:54 PM

Quote:

Originally Posted by GrouchyAdmin (Post 16293451)
Jeeesus peoples. Can this go away now pls?

buttsecks???!!! :D

GrouchyAdmin 09-08-2009 02:57 PM

Quote:

Originally Posted by nation-x (Post 16293576)
buttsecks???!!! :D

I gave at the office. http://img216.yfrog.com/img216/6622/emotsmug.png

Killswitch - BANNED FOR LIFE 09-08-2009 03:04 PM

Quote:

Originally Posted by GrouchyAdmin (Post 16293451)
Jeeesus peoples. This is a hack of a slightly-better-than-my-above for just checking for the files' existence, based upon code at PHP.net; I figured this thread should die a horrible death, so here ya go.

Code:

function http_file_exists($url=FALSE) {
  // Bad URL, bail.
  if (!(@parse_url($url))) return FALSE;
  // No cURL, bail.
  if (!(function_exists('curl_init'))) return FALSE;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_NOBODY, 1);
  curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  return (curl_exec($ch) !== FALSE) ? TRUE : FALSE;
}

Here's even a (shitty) working example:

Code:

$urltest = array ("http://www.google.com/logo.gif", "http://www.google.com/intl/en_ALL/images/logo.gif");

foreach ($urltest as $url) {
  echo http_file_exists($url) ? "$url exists.\n" : "$url is a 404.\n";
}
exit;

Code:

http://www.google.com/logo.gif is a 404.
http://www.google.com/intl/en_ALL/images/logo.gif exists.

Can this go away now pls?

No, I refuse to let it! BUMMMPPP!!

GrouchyAdmin 09-08-2009 03:09 PM

Quote:

Originally Posted by Killswitch (Post 16293635)
No, I refuse to let it! BUMMMPPP!!

I cutchoo mang. I cutchoo so bad you wish I no cutchoo. http://img178.imageshack.us/img178/7753/emotese.gif


...and I'm gonna keep using a banned imagehost until this damn thing goes away. S'there!

Killswitch - BANNED FOR LIFE 09-08-2009 03:10 PM

Quote:

Originally Posted by GrouchyAdmin (Post 16293662)
I cutchoo mang. I cutchoo so bad you wish I no cutchoo. http://img178.imageshack.us/img178/7753/emotese.gif


...and I'm gonna keep using a banned imagehost until this damn thing goes away. S'there!

I cut you back... I cut you deep, I cut you so deep, yo momma be like "WTF IS THAT!?!?".

Imageshack works on gfy.com, fail.

GrouchyAdmin 09-08-2009 03:12 PM

Quote:

Originally Posted by Killswitch (Post 16293672)
Imageshack works on gfy.com, fail.

LOS GROUCHY KICK YOUR ASS
LOS GROUCHY KICK YOUR FACE
LOS GROUCHY http_file_exists() IN TO OUTER SPACE

Dammit. It used to be banned as far back as.. two weeks ago.

munki 09-08-2009 03:13 PM


Killswitch - BANNED FOR LIFE 09-08-2009 03:28 PM

Quote:

Originally Posted by GrouchyAdmin (Post 16293681)
LOS GROUCHY KICK YOUR ASS
LOS GROUCHY KICK YOUR FACE
LOS GROUCHY http_file_exists() IN TO OUTER SPACE

Dammit. It used to be banned as far back as.. two weeks ago.

LOS GROUCHY KICK ROCKS
LOS GROUCHY NO HURT
LOS GROUCHY http_file_exists() RETURN ERROR FOR SUCKAGE

Hmm, weird, that little smiley you posted from imageshack showed for me on gfy.com

GrouchyAdmin 09-08-2009 03:30 PM

Quote:

Originally Posted by Killswitch (Post 16293745)
LOS GROUCHY KICK ROCKS
LOS GROUCHY NO HURT
LOS GROUCHY http_file_exists() RETURN ERROR FOR SUCKAGE

Hmm, weird, that little smiley you posted from imageshack showed for me on gfy.com

Yeah, well, you're Retox_Josh.

Feel that burn, motherbitch. Feel it!? Mmmyeah. That's the stuff.

quantum-x 09-08-2009 03:42 PM

return (!file_get_contents('URL')?false:true);
Do I win ?

Killswitch - BANNED FOR LIFE 09-08-2009 03:51 PM

Quote:

Originally Posted by GrouchyAdmin (Post 16293757)
Yeah, well, you're Retox_Josh.

Feel that burn, motherbitch. Feel it!? Mmmyeah. That's the stuff.

Touché

You win this time my little pretty, but I will get you and your dog too!

GrouchyAdmin 09-08-2009 03:52 PM

Quote:

Originally Posted by quantum-x (Post 16293842)
return (!file_get_contents('URL')?false:true);
Do I win ?

No. Michael P wins for 'not reading the fucking thread at all', but nice try.

Quote:

Originally Posted by Killswitch (Post 16293894)
Touché

You win this time my little pretty, but I will get you and your dog too!

Surrender Grouchy, my ass.

quantum-x 09-08-2009 03:59 PM

Quote:

Originally Posted by GrouchyAdmin (Post 16293902)
No. Michael P wins for 'not reading the fucking thread at all', but nice try.



Surrender Grouchy, my ass.

Damnit .

munki 09-08-2009 04:02 PM


GrouchyAdmin 09-08-2009 04:04 PM

Quote:

Originally Posted by quantum-x (Post 16293959)
Damnit .

You do get a few points for ternary abuse, though. :thumbsup

quantum-x 09-08-2009 04:30 PM

Quote:

Originally Posted by GrouchyAdmin (Post 16293980)
You do get a few points for ternary abuse, though. :thumbsup

If you cram it aaaalllll onto one line, if the script ever breaks, at least you know the line to debug :1orglaugh


All times are GMT -7. The time now is 03:07 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123