Checking existence of a file/URL

How can I tell if a certain URL exists? I see there are a few ways to do this with PHP but I can’t get any of them to work with an absolute URL. I can check for files on my domain using a relative path, but not with an absolute path.

Also, the file I’m checking for is on a subdomain of the domain where the PHP file lives that is executing the check, and the subdomain is not on the same web server. I CAN NOT use response headers in my case.

I’m executing filecheck.php from https://myserver.com, checking for a file at http://subdomain/myserver.com.

Example from PHP.net fopen reference:
$handle = fopen("http://www.example.com/", "r");
I can get this to work with “/myfile.txt”, but not with “https://myserver.com/myfile.txt”.

I’d appreciate some help.

Try checking if the URL exists using this tool. Selecting “simple” uses curl and “verbose” something like PHP header.

http://supiet2.tk/

Edit:
Just checked and “verbose” selection uses the following:

https://secure.php.net/manual/en/function.get-headers.php

Just realised that if the files are on your server and you have access then try the following:

file: “test-to-find-path.php”

<?php 
// in the same folder/path as “https://myserver.com/myfile.txt”
echo '<br> getcwd() ==> '    .getcwd();
echo '<br> __DIR__ ==> '   . __dir__; 
echo '<br> __FILE__ ==> '  . __file__; // this file: "test-to-find-path.php"
// Test:
  $myFileDotTxt = 'latest.php';
  $myFullPath   = __DIR__ .'/' .$myFileDotTxt;

  if( file_exists( __DIR__ .'/' .$myFileDotTxt ) ):
    echo '<br> Yes - found file: ';
  else:
    echo 'Unable to find file: ';
  endif;
  echo $myFileDotTxt;

Thanks, John. It appears my setting for “allow_url_fopen” in my file’s folder php.ini was not being honored. I had to set it in the root’s php.ini.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.