[PHP-DEV] new FTP function

2013-01-18 Thread KISE
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.


Re: [PHP-DEV] new FTP function

2013-01-18 Thread Will Fitch

On Jan 18, 2013, at 9:53 AM, KISE wowk...@gmail.com 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.

Re: [PHP-DEV] new FTP function

2013-01-18 Thread Paul Dragoonis
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 willfi...@php.net wrote:


 On Jan 18, 2013, at 9:53 AM, KISE wowk...@gmail.com 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.


Re: [PHP-DEV] new FTP function

2013-01-18 Thread KISE
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 dragoo...@gmail.com 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 willfi...@php.net wrote:


 On Jan 18, 2013, at 9:53 AM, KISE wowk...@gmail.com 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.





Re: [PHP-DEV] new FTP function

2013-01-18 Thread Paul Dragoonis
On Fri, Jan 18, 2013 at 3:18 PM, KISE wowk...@gmail.com 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);
}


Re: [PHP-DEV] new FTP function

2013-01-18 Thread Paul Dragoonis
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 dragoo...@gmail.com wrote:


 On Fri, Jan 18, 2013 at 3:18 PM, KISE wowk...@gmail.com 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);
 }



Re: [PHP-DEV] new FTP function

2013-01-18 Thread Paul Dragoonis
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 :-)


On Fri, Jan 18, 2013 at 3:30 PM, Paul Dragoonis dragoo...@gmail.com wrote:

 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 dragoo...@gmail.comwrote:


 On Fri, Jan 18, 2013 at 3:18 PM, KISE wowk...@gmail.com 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);
 }





Re: [PHP-DEV] new FTP function

2013-01-18 Thread KISE
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 dragoo...@gmail.com wrote:


 On Fri, Jan 18, 2013 at 3:18 PM, KISE wowk...@gmail.com 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);
 }



Re: [PHP-DEV] new FTP function

2013-01-18 Thread Marco Pivetta
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/


On 18 January 2013 16:33, KISE wowk...@gmail.com wrote:

 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 dragoo...@gmail.com
 wrote:

 
  On Fri, Jan 18, 2013 at 3:18 PM, KISE wowk...@gmail.com 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);
  }
 



Re: [PHP-DEV] new FTP function

2013-01-18 Thread KISE
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.


On Fri, Jan 18, 2013 at 6:37 PM, Marco Pivetta ocram...@gmail.com wrote:

 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/


 On 18 January 2013 16:33, KISE wowk...@gmail.com wrote:

 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 dragoo...@gmail.com
 wrote:

 
  On Fri, Jan 18, 2013 at 3:18 PM, KISE wowk...@gmail.com 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);
  }