file_exists and array_merge_clobber

php.internals

Lukas Smith

22 years ago
Hi, We have a new intern who got right to work to improve file_exists() to also be able to check the include_path similar to how fopen() can. We have a patch here. Due to some issues on the CVS version of ext/standard/filestat.c in HEAD we commented out some code in the file in order to be able to compile. This is not really needed for our patch of course. You can find the patch here: http://www.backendmedia.com/php_patches/file_exists.patch Since neither my intern nor I have previous experience in modifying the PHP source we would obviously appreciate any hints. We were especially unsure how much we need to check the parameter values ourselves. We also plan to create a new array function based on a user suggestion in the user comments of array_merge_recursive(). Here is an implementation in PHP (I modified it a bit this morning without testing it in much detail, so hopefully it works as I have planned it). Essentially I need this in a bunch of PEAR classes to be able to handle multidimensional option arrays that need to overwrite a default array. function array_merge_clobber($a1, $a2) { if (!is_array($a1) || !is_array($a2)) { return false; } foreach($a2 as $key => $val) { if (is_array($val) && isset($a1[$key]) && is_array($a1[$key]) ) { $a1[$key] = array_merge_clobber($a1[$key], $val); } else { $a1[$key] = $val; } } return $a1; } Dunno if the name is all that good though :-) regards, Lukas Smith smith@backendmedia.com _______________________________ BackendMedia www.backendmedia.com berlin@backendmedia.com Linn Zwoch Smith GbR Pariser Str. 44 D-10707 Berlin Tel +49 30 83 22 50 00 Fax +49 30 83 22 50 07

Wez Furlong

22 years ago
Using the include path for file_exists doesn't sound useful to me, for similar reasons as those for not allowing to write a file in the include path - which file are you actually writing to? What if someone puts a file with the same name earlier in the include path and so on. What is probably better is some kind of generic path-searching function that returns the full path to the first file in the include_path (or some other path). --Wez. ----- Original Message ----- From: "Lukas Smith" <smith@backendmedia.com> To: <internals@lists.php.net> Sent: Tuesday, September 30, 2003 11:32 AM Subject: [PHP-DEV] file_exists and array_merge_clobber

Lukas Smith

22 years ago
> From: Wez Furlong [mailto:wez@thebrainroom.com] > Sent: Tuesday, September 30, 2003 12:58 PM
> Using the include path for file_exists doesn't sound useful to me, for > similar reasons as those for not allowing to write a file in the
include
> path - which file are you actually writing to? What if someone puts a > file > with the same name earlier in the include path and so on. > > What is probably better is some kind of generic path-searching
function
> that > returns the full path to the first file in the include_path (or some
other
> path). > > --Wez. > > ----- Original Message ----- > From: "Lukas Smith" <smith@backendmedia.com> > To: <internals@lists.php.net> > Sent: Tuesday, September 30, 2003 11:32 AM > Subject: [PHP-DEV] file_exists and array_merge_clobber > > > > Hi, > > > > We have a new intern who got right to work to improve file_exists()
to
> > also be able to check the include_path similar to how fopen() can.
In my case I had some code that loaded a module by name and modules can have different structures. So currently I do @include(). And I just thought it would be cleaner to be able to check beforehand. Hartmut just said on IRC it might be nicer to have a path as an optional parameter. Maybe it would be ok to simply be able to pass a number of paths at once. Then I could go ini_get('include_path'). However it is probably nicer being able to pass multiple paths as separate parameters instead of one with a separator between the various paths. Regards, Lukas

Justin Hannus

22 years ago
I agree with Wez, what if you are searching for a file with a relative file path, e.g: <?php if (file_exists("somefile.php")) // ... would return true if "somefile.php" is not in the current directory but is in your include path, not what you expected. Besides, you can easily implement this functionality in php. -Justin Hannus "Wez Furlong" <wez@thebrainroom.com> wrote in message news:021801c38741$b34407f0$0702a8c0@titan...
> Using the include path for file_exists doesn't sound useful to me, for > similar reasons as those for not allowing to write a file in the include > path - which file are you actually writing to? What if someone puts a
file
> with the same name earlier in the include path and so on. > > What is probably better is some kind of generic path-searching function
that

Lukas Smith

22 years ago
> From: Justin Hannus [mailto:jhannus@visualconceptsinc.com] > Sent: Tuesday, September 30, 2003 8:12 PM > > I agree with Wez, what if you are searching for a file with a relative > file > path, e.g: > > <?php > if (file_exists("somefile.php")) > // ... > > would return true if "somefile.php" is not in the current directory
but is
> in your include path, not what you expected. Besides, you can easily > implement this functionality in php.
Uhm .. there is a parameter to control of you want to search your include path which defaults to off. Finally as an alternative I said I would also be fine if I could pass multiple path's to check to file_exits(). Regards, Lukas

Justin Hannus

22 years ago
Aahh, I didn't see that in your original message :) "Lukas Smith" <smith@backendmedia.com> wrote in message news:000a01c3877f$955a5f30$4d00a8c0@vandal...

Hartmut Holzgraefe

22 years ago
Justin Hannus wrote:
> I agree with Wez, what if you are searching for a file with a relative file > path, e.g: > > <?php > if (file_exists("somefile.php")) > // ... > > would return true if "somefile.php" is not in the current directory but is > in your include path, not what you expected. Besides, you can easily > implement this functionality in php.
its going to be optional, not mandatory ... but that has already been answered the whole point about this additional feature is that you can use it to check for include file existance before the actual include: if (file_exists("somefile.php", true)) { include "somefile.php"; } IMHO this case is common enough so that it deserves a native C implementation either as an option to file_exists() or as a seperate function like file_exists_in_path()
-- Hartmut Holzgraefe <hartmut@php.net>

Jon Parise

22 years ago
On Wed, Oct 01, 2003 at 10:00:23AM +0200, Hartmut Holzgraefe wrote:
> the whole point about this additional feature is that you can use it to > check > for include file existance before the actual include: > > if (file_exists("somefile.php", true)) { > include "somefile.php"; > } > > IMHO this case is common enough so that it deserves a native C > implementation > either as an option to file_exists() or as a seperate function like > file_exists_in_path()
If we're really going to go down this road, why not implement it in a more generic, reusable way: file_exists("somefile.php", ini_get('include_path')) That would give us some additional mileage out of the change. For the record, I'm -0 on this. I'd be -1 if it wasn't so obviously optional.
-- Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)

Lukas Smith

22 years ago
> From: Jon Parise [mailto:jon@php.net] > Sent: Wednesday, October 01, 2003 10:07 AM
> On Wed, Oct 01, 2003 at 10:00:23AM +0200, Hartmut Holzgraefe wrote: > > > the whole point about this additional feature is that you can use it
to
> > check > > for include file existance before the actual include: > > > > if (file_exists("somefile.php", true)) { > > include "somefile.php"; > > } > > > > IMHO this case is common enough so that it deserves a native C > > implementation > > either as an option to file_exists() or as a seperate function like > > file_exists_in_path() > > If we're really going to go down this road, why not implement it in a > more generic, reusable way: > > file_exists("somefile.php", ini_get('include_path')) > > That would give us some additional mileage out of the change. > > For the record, I'm -0 on this. I'd be -1 if it wasn't so obviously > optional.
Yeah that is what I suggested yesterday. I can see two scenarios for this: 1) Check infront of an include 2) Simply check if a file exists in a list of paths For 1) it would be better to pass the path optionally with the ini separator inbetween the path list in order to be able to simply pass the result of ini_get('include_path') For 2) it would probably be each to be able to pass each path separately or as an array. Obviously you can do both in one function, dunno if that is really clean either. But either way it _must_ be optional of course. Regards, Lukas

Greg Beaver

22 years ago
In keeping with other functions that test pseudo-types like is_callable(), why not create a new function is_includeable() that will take a filename/path, and use include_path by default, or accept an optional array of paths to search. if (is_includeable("somefile.php")) { include "somefile.php"; } This will avoid any confusion with file_exists and more clearly define the purpose. In addition, it would allow people whose hosts have put ini_get() and get_include_path() in php.ini's disabled_functions setting to still test for inclusion potential, and safely. Regards, Greg

Hartmut Holzgraefe

22 years ago
Greg Beaver wrote:
> In keeping with other functions that test pseudo-types like > is_callable(), why not create a new function is_includeable() that will > take a filename/path, and use include_path by default, or accept an > optional array of paths to search. > > if (is_includeable("somefile.php")) { > include "somefile.php"; > } >
this would also be confusing as it would behave different like is_readable() etc. esp. for is_executable() it would not be clear wheter it behaves like is_readable() (checking just relative to the current work dir.) or like is_includeable() (checking for existance in $_ENV[PATH])
-- Hartmut Holzgraefe <hartmut@php.net>