# new FTP function

- Thread: https://externals.io/message/65023
- Original archive: https://news-web.php.net/php.internals/65023
- Started by KISE on 2013-01-18T14:53:51+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #65023 — KISE — 2013-01-18T14:53:51+00:00

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

Hi

II would like to see "ftp_dir_exists()" function in PHP, right now its
kinda unreasonable to expect people to use hacks such as "is_dir()" and
having to re-authenticate just to check if the dir exists, I also dont
think its good idea to use "ftp_chdir()" just to check if the directory
exists, because its shows errors if the directory doesn't exists. i think
there should be a way to check if the directory exists without having to
resort to hackish ways, or having to authenticate again.

---

## #65024 — Will Fitch — 2013-01-18T15:00:05+00:00

URL: https://externals.io/message/65023#65024 · In reply to: #65023

> Hi
> 
> II would like to see "ftp_dir_exists()" function in PHP, right now its
> kinda unreasonable to expect people to use hacks such as "is_dir()" and
> having to re-authenticate just to check if the dir exists, I also dont
> think its good idea to use "ftp_chdir()" just to check if the directory
> exists, because its shows errors if the directory doesn't exists. i think
> there should be a way to check if the directory exists without having to
> resort to hackish ways, or having to authenticate again.

There are procedures in place for this.  If you'd like to make a feature request, use bugs.php.net and submit the request with the FTP extension selected.  You can also use https://wiki.php.net/rfc if you really want to see/make the change as well.

---

## #65025 — Paul Dragoonis — 2013-01-18T15:04:54+00:00

URL: https://externals.io/message/65023#65025 · In reply to: #65024

I don't believe there's a feature of the FTP protocol to check if a
file/folder exists.

Can you please provide a PHP userland solution to this, so that a
corresponding C implementation could be added.

I believe the way to go about it is list the CWD contents and check if that
exists and is of type DIR. You must also make sure that if a file exists
with the name of the directory you want to check it has to handle that
properly, it can't simply return true or false. If false then that means
you could create it because it doesn't exist. If true it means you can CD
into it and put stuff there, which is also not true.

Thanks,
Paul.




>
> 
>
> > […]
>
> There are procedures in place for this.  If you'd like to make a feature
> request, use bugs.php.net and submit the request with the FTP extension
> selected.  You can also use https://wiki.php.net/rfc if you really want
> to see/make the change as well.

---

## #65026 — KISE — 2013-01-18T15:18:50+00:00

URL: https://externals.io/message/65023#65026 · In reply to: #65025

Will Fitch, sorry i didn't know that i contacted one of the dev to post it
on behalf of me and he said its ok to post here, i'll post it there right
away

Paul Dragoonis, there is 3 ways to do it in userland codes.

    Public function dir_exists($dir)
    {
        if (!$this->conn_id)
            return false;

        $currentDir = ftp_pwd($this->conn_id);

        if ( ftp_chdir($this->conn_id, $dir) )
        {
            ftp_chdir($this->conn_id, $currentDir);
            return true;
        }

        return false;
    }

this function above attempts to change directory it see if its exists or
not. and produce `E_WARNING` if the dir doesn't exists

and there is

echo ( is_dir("ftp://{$username}:{$password}@{$server}{$dir}") ) ? true :
false;

which require that you re-authenticate

    Public function dir_exists2($dir)
    {
        if ( !$this->conn_id )
            return false;

        $orginal = $dir;

        if ( substr($dir, -1) === '/' )
            $dir = substr($dir, 0, strlen($dir)-1);

        $dir    = substr($dir, 0, strlen($dir)-strlen(strrchr($dir,'/')));

        $res  = ftp_nlist($this->conn_id, '-dF '. $dir);

        if ( isset($res) AND is_array($res) )
        {
            foreach ($res as $key => $value)
            {
                if ($value === $orginal)
                    return true;
            }
        }
        return false;
    }

this function i hacked my self to stop the `E_WARNING` from flooding my error
log files. it works but kinda ugly and so hackish, and i dont think it will
work nicely if there is too many directories.






> I don't believe there's a feature of the FTP protocol to check if a
> file/folder exists.
>
> Can you please provide a PHP userland solution to this, so that a
> corresponding C implementation could be added.
>
> I believe the way to go about it is list the CWD contents and check if
> that exists and is of type DIR. You must also make sure that if a file
> exists with the name of the directory you want to check it has to handle
> that properly, it can't simply return true or false. If false then that
> means you could create it because it doesn't exist. If true it means you
> can CD into it and put stuff there, which is also not true.
>
> Thanks,
> Paul.
>
>
> 
>
> > […]

---

## #65027 — Paul Dragoonis — 2013-01-18T15:28:30+00:00

URL: https://externals.io/message/65023#65027 · In reply to: #65026

>         $res  = ftp_nlist($this->conn_id, '-dF '. $dir);
>

This could be done in two lines.

function dir_exists($conn, $dir) {
        $list  = ftp_nlist($conn, '-dF '. $dir);
        return in_array($dir, $list);
}

---

## #65028 — Paul Dragoonis — 2013-01-18T15:30:39+00:00

URL: https://externals.io/message/65023#65028 · In reply to: #65027

Revised function:

function dir_exists($conn, $currentDir) {
        $list  = ftp_nlist($conn, '-dF '. $currentDir);
        return in_array($dir, $list);
}




>
> 
>
> > […]
>
> This could be done in two lines.
>
> function dir_exists($conn, $dir) {
>         $list  = ftp_nlist($conn, '-dF '. $dir);
>         return in_array($dir, $list);
> }

---

## #65029 — Paul Dragoonis — 2013-01-18T15:31:37+00:00

URL: https://externals.io/message/65023#65029 · In reply to: #65028

An extra parameter is required in the above function, but you get the
point, i don't want to keep spamming the mailing list with corrections :-)




> Revised function:
>
> function dir_exists($conn, $currentDir) {
>         $list  = ftp_nlist($conn, '-dF '. $currentDir);
>         return in_array($dir, $list);
> }
>
>
> On Fri, Jan 18, 2013 at 3:28 PM, Paul Dragoonis <dragoonis@gmail.com>wrote:
>
> > […]

---

## #65030 — KISE — 2013-01-18T15:33:59+00:00

URL: https://externals.io/message/65023#65030 · In reply to: #65027

Paul Dragoonis,

Actually it wont work i did tried it before, if the dir end with / it will
list the directories inside the path you gave it and if it doesn't have any
directories it will return false since there is no directories to return.

you have to take out the last / and then remove the directory in question
and list the files in the parent directory and check if the dir exists
otherwise it will return false, i spent 3hrs yesterday thinking why its
returning false even though the directory exists.



>
> 
>
> > […]
>
> This could be done in two lines.
>
> function dir_exists($conn, $dir) {
>         $list  = ftp_nlist($conn, '-dF '. $dir);
>         return in_array($dir, $list);
> }

---

## #65031 — Marco Pivetta — 2013-01-18T15:37:36+00:00

URL: https://externals.io/message/65023#65031 · In reply to: #65030

Heya KISE,

I just checked a bit around and found that current implementations actually
simply deal with the warning:

https://github.com/rjkip/ftp-php/blob/master/src/FtpPhp/FtpClient.php#L172-L186
https://github.com/rjkip/ftp-php/blob/master/src/FtpPhp/FtpClient.php#L92-L116

What kind of problems is the warning currently causing in your code?

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/




> Paul Dragoonis,
>
> Actually it wont work i did tried it before, if the dir end with / it will
> list the directories inside the path you gave it and if it doesn't have any
> directories it will return false since there is no directories to return.
>
> you have to take out the last / and then remove the directory in question
> and list the files in the parent directory and check if the dir exists
> otherwise it will return false, i spent 3hrs yesterday thinking why its
> returning false even though the directory exists.
>
> On Fri, Jan 18, 2013 at 6:28 PM, Paul Dragoonis <dragoonis@gmail.com>
> wrote:
>
> > […]

---

## #65032 — KISE — 2013-01-18T15:44:44+00:00

URL: https://externals.io/message/65023#65032 · In reply to: #65031

hello Marco Pivetta,

while the implementations you linked indeed works, it also kinda big and
involve changing the error handler just so that it doesn't show the error,
which i think is not the way to do it. i think there should be simple way
to check like "is_dir" or "is_file".

anyway thanks for the hints, i have sent request to bugs.php.net, and i
dont like to spam the dev list since they quite busy already

PS: and sorry for the grammar errors, English is not my native tongue.




> Heya KISE,
>
> I just checked a bit around and found that current implementations
> actually simply deal with the warning:
>
>
> https://github.com/rjkip/ftp-php/blob/master/src/FtpPhp/FtpClient.php#L172-L186
>
> https://github.com/rjkip/ftp-php/blob/master/src/FtpPhp/FtpClient.php#L92-L116
>
> What kind of problems is the warning currently causing in your code?
>
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://ocramius.github.com/
>
>
> 
>
> > […]
