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.
On Fri, Jan 18, 2013 at 6:04 PM, Paul Dragoonis <[email protected]> wrote:
> 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.
>
>
> On Fri, Jan 18, 2013 at 3:00 PM, Will Fitch <[email protected]> wrote:
>
>>
>> On Jan 18, 2013, at 9:53 AM, KISE <[email protected]> wrote:
>>
>> > 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.
>
>
>