# file_exists and array_merge_clobber

- Thread: https://externals.io/message/4610
- Original archive: https://news-web.php.net/php.internals/4610
- Started by Lukas Smith on 2003-09-30T10:32:59+00:00
- Messages: 5
- 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

---

## #4610 — Lukas Smith — 2003-09-30T10:32:59+00:00

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

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

---

## #4611 — Wez Furlong — 2003-09-30T10:57:54+00:00

URL: https://externals.io/message/4610#4611 · In reply to: #4610

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.

---

## #4621 — Justin Hannus — 2003-09-30T18:11:50+00:00

URL: https://externals.io/message/4610#4621 · In reply to: #4611

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
> 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
>
>
> > […]

---

## #4629 — Hartmut Holzgraefe — 2003-10-01T08:00:23+00:00

URL: https://externals.io/message/4610#4629 · In reply to: #4621

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

---

## #4630 — Jon Parise — 2003-10-01T08:06:51+00:00

URL: https://externals.io/message/4610#4630 · In reply to: #4629

> 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/)
