Jump to content

Recommended Posts

I have a decent script for checking if a file exists. However it's not working properly for a guide I uploaded to the same website. So can you help me improve my script, maybe make it looks nicer as well?

 

function urlcheck($flink)
{   
  $link = str_replace(" ","%20",$flink);
  $url_parts = @parse_url( $link );

  if ( empty( $url_parts["host"] ) ) return( false );

  if ( !empty( $url_parts["path"] ) )
    $documentpath = $url_parts["path"];
  else
    $documentpath = "/";

  if ( !empty( $url_parts["query"] ) )
    $documentpath .= "?" . $url_parts["query"];

  $host = $url_parts["host"];
  $port = $url_parts["port"];

  if (empty( $port ) ) $port = "80";
  $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );

  if (!$socket)
    return false;
  else
{
	fwrite ($socket, "GET ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
     
     $http_response = fgets( $socket, 50 );
       
     if ( ereg("200 OK", $http_response, $regs ) )
     {
        fclose( $socket );
			return true;
     } 
	else 
       		return false;
  } 
}

 

http://openzelda.thegaminguniverse.com/guides/KOHTutorial/index.html (says this does not exist) on my openzelda site.

Link to comment
https://forums.phpfreaks.com/topic/198029-file-exists-no-matter-the-site/
Share on other sites

I came up with my own method which seems to solve my problem so far.

 

function urlcheck($flink)
{   
  $link = str_replace(" ","%20",$flink);
  $url_parts = @parse_url( $link );
  
  if(substr($url_parts["path"],2,27)== "openzelda.thegaminguniverse")
  {
$ozfile = substr($url_parts["path"],34);
if(file_exists($ozfile))
	return true;
  }
  


  if ( empty( $url_parts["host"] ) ) return( false );

  if ( !empty( $url_parts["path"] ) )
    $documentpath = $url_parts["path"];
  else
    $documentpath = "/";

  if ( !empty( $url_parts["query"] ) )
    $documentpath .= "?" . $url_parts["query"];

  $host = $url_parts["host"];
  $port = $url_parts["port"];

  if (empty( $port ) ) $port = "80";
  $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );

  if (!$socket)
    return false;
  else
{
	fwrite ($socket, "GET ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
     
     $http_response = fgets( $socket, 50 );
       
     if ( ereg("200 OK", $http_response, $regs ) )
     {
        fclose( $socket );
			return true;
     } 
	else 
       		return false;
  } 
}

 

To me it's simple and I easily understand it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.