You are replacing /project/ with a space and this will output filename like " index.php" with a preceding space.
$pagename = str_replace('/project/'," ",$page);
you need to replace it without any space like
$pagename = str_replace('/project/',"",$page);
this will output filename without space.
$pagename
? – Chirag Chhuchha