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.
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.
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.
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.
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.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.
> $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);
}
function dir_exists($conn, $currentDir) {
$list = ftp_nlist($conn, '-dF '. $currentDir);
return in_array($dir, $list);
}
>
>
>
>> $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);
> }
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
>
>>
>>
>>
>>> $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);
>> }
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.
>
>
>
>> $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);
> }
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
> wrote:
>
> >
> >
> >
> >> $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);
> > }
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/
>
>
>
>
>> 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
>> wrote:
>>
>> >
>> >
>> >
>> >> $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);
>> > }