# Feature Request

- Thread: https://externals.io/message/57185
- Original archive: https://news-web.php.net/php.internals/57185
- Started by Keloran on 2012-01-04T16:18:54+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #57185 — Keloran — 2012-01-04T16:18:54+00:00

URL: https://externals.io/message/57185#57185

I dont seem to have the karma for doing this in RFC so ill propose it here,
and once karma make an RFC

Is it possible to have a 2nd option on file_exists so that it returns the
path you specified

--Example
$cFile = file_exists("/file/located/here.php", true);

and if that exists it will return
/file/located/here.php

instead of true

--Reason
the reason for this is simple actually at the moment if you want to include
a file for usage but want to check it exists you have todo the following
if (file_exists("/file/located/here.php")) {
 $cFile = "/file/located/here.php";
}

which can if your doing lots of checking alot of extra code for no reason,
or if you want to use variables, lots of louse variables just for something
that could be pulled from the request

---

## #57186 — Rafael Kassner — 2012-01-04T16:27:41+00:00

URL: https://externals.io/message/57185#57186 · In reply to: #57185

realpath function do this for you. It returns the absolute path, if
file exists. Otherwise, returns null.


> I dont seem to have the karma for doing this in RFC so ill propose it here,
> and once karma make an RFC
>
> Is it possible to have a 2nd option on file_exists so that it returns the
> path you specified
>
> --Example
> $cFile = file_exists("/file/located/here.php", true);
>
> and if that exists it will return
> /file/located/here.php
>
> instead of true
>
> --Reason
> the reason for this is simple actually at the moment if you want to include
> a file for usage but want to check it exists you have todo the following
> if (file_exists("/file/located/here.php")) {
>  $cFile = "/file/located/here.php";
> }
>
> which can if your doing lots of checking alot of extra code for no reason,
> or if you want to use variables, lots of louse variables just for something
> that could be pulled from the request



-- 
Atenciosamente,
Rafael Kassner

---

## #57187 — Ferenc Kovacs — 2012-01-04T16:34:25+00:00

URL: https://externals.io/message/57185#57187 · In reply to: #57185

> I dont seem to have the karma for doing this in RFC so ill propose it here,
> and once karma make an RFC
>
> Is it possible to have a 2nd option on file_exists so that it returns the
> path you specified
>
> --Example
> $cFile = file_exists("/file/located/here.php", true);
>
> and if that exists it will return
> /file/located/here.php
>
> instead of true
>
> --Reason
> the reason for this is simple actually at the moment if you want to include
> a file for usage but want to check it exists you have todo the following
> if (file_exists("/file/located/here.php")) {
>  $cFile = "/file/located/here.php";
> }
>
> which can if your doing lots of checking alot of extra code for no reason,
> or if you want to use variables, lots of louse variables just for something
> that could be pulled from the request
>

as others mentioned that you can use realpath for that.
However realpath doesn't support resolving against the include path, so
depending on your usecase
http://php.net/manual/en/function.stream-resolve-include-path.php  would be
a better idea.
This still doesn't solve the potential race condition in your example (the
file could have been deleted or moved between your check and the actual
include/open call.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #57218 — keisial@gmail.com — 2012-01-05T01:07:26+00:00

URL: https://externals.io/message/57185#57218 · In reply to: #57185

> which can if your doing lots of checking alot of extra code for no reason,
> or if you want to use variables, lots of louse variables just for something
> that could be pulled from the request
I don't  think it would mean 'a lot of code', but if it bothers you in 
your app, just create a wrapper:

function keloran_file_exists($filename) {
   return file_exists($filename) ? $filename : false;
}
