Here is an example…
//..path/to/wp-content/plugins/your-plugin-basedir/
$path = plugin_dir_path( __FILE__);
//..directory within your plugin
$path .= 'templates';
//..continue with your script...
$dir_handle = @opendir($path) or die("Cannot open the damn file $path");
UPDATE
I tested the rest of your script by the way, to see if it in fact did read the directory and list files with the *.php extension and it wouldn’t work.
Instead after modifying it to;
$dir_handle = @opendir($path) or die("Cannot open the damn file $path");
while ($file = readdir($dir_handle)) {
//get length of filename inc. extension
$length_of_filename = strlen($file);
//strip all but last three characters from file name to get extension
$ext = substr($file, -3, $length_of_filename);
if($ext == "php" ) {
$TheLinkedFile = $path."https://wordpress.stackexchange.com/".$file;
if(file_exists($TheLinkedFile)) {
echo $TheLinkedFile.'<br>';
} else {
echo "nothing";
}
}
}
closedir($dir_handle);
It works…