如何使用 PHP FTP 功能检查服务器上是否存在文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1554346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:03:18  来源:igfitidea点击:

How to check using PHP FTP functionality if folder exists on server or not?

php

提问by Elitmiar

Is there a way to check if a a folder exists on the server using PHP Ftp functionality?

有没有办法使用 PHP Ftp 功能检查服务器上是否存在 aa 文件夹?

回答by Piskvor left the building

For PHP 5:

对于 PHP 5:

$folder_exists = is_dir('ftp://user:[email protected]/some/dir/path');

http://php.net/manual/en/function.is-dir.php: "As of PHP 5.0.0, this function can also be used with some URL wrappers."

http://php.net/manual/en/function.is-dir.php:“从 PHP 5.0.0 开始,这个函数也可以与一些 URL 包装器一起使用。”

http://php.net/manual/en/wrappers.ftp.php: [Support] "As of PHP 5.0.0: filesize(), filetype(), file_exists(), is_file(), and is_dir()"

http://php.net/manual/en/wrappers.ftp.php:[支持]“自 PHP 5.0.0 起:filesize()、filetype()、file_exists()、is_file() 和 is_dir()”

回答by Claytinho

Try this:

尝试这个:

if (ftp_nlist($ftp_stream, $new_folder) == false) {
    ftp_mkdir($ftp_stream, $new_folder);
}

回答by Ewout

There is no 'exists' function for ftp connections in php.

php 中的 ftp 连接没有“存在”功能。

You could try ftp_chdirand check the result

您可以尝试ftp_chdir并检查结果

回答by Evyatar Nezer

stumbled upon this question from 2009 and found that question unsatisfying for me. I've done little research of my own a found a little tweak for that issue.

从 2009 年偶然发现了这个问题,发现这个问题让我不满意。我自己做了一点研究,发现了针对该问题的一些调整。

So if found the *ftp_nlist*function witch returns an array of stringwith files and folders names witch exist under the current ftp directory, and then I've simply checked under the array (with foreacah loop) if the folder or file we searched for exist there and an new ifExist method as been created.

因此,如果发现*ftp_nlist*函数女巫返回一个字符串数组,其中文件和文件夹名称巫婆存在于当前 ftp 目录下,然后我只是在数组下检查(使用 foreacah 循环)我们搜索的文件夹或文件存在那里并创建了一个新的 ifExist 方法。

you can read more at php.net: http://php.net/manual/en/function.ftp-nlist.php

您可以在 php.net 阅读更多信息:http: //php.net/manual/en/function.ftp-nlist.php

another option for the YII frameworkusers its the ftp extension that masks the php ftp lib and makes it even easier to work with an ftp server from code.

YII 框架用户的另一个选择是它的 ftp 扩展,它屏蔽了 php ftp 库,使从代码使用 ftp 服务器变得更加容易。

Hope you'l find helpful.

希望你会觉得有帮助。

回答by Lackeeee

The solution that works for me:

对我有用的解决方案:

if (!in_array($dirName.'/'.$something, $ftp->nlist($dirName))) {  
   // do something
}             

回答by Eugen Mihailescu

ftp_mdtm()gets the last modified time for a remote file. It returns the last modified time as a Unix timestamp on success, or -1 on error. Note that ftp_mdtm() does not work with directories. So if we get -1 then maybe the file is directory.

ftp_mdtm()获取远程文件的最后修改时间。成功时返回最后修改时间作为 Unix 时间戳,错误时返回 -1。请注意, ftp_mdtm() 不适用于目录。因此,如果我们得到 -1,那么该文件可能是目录。

Note also that not all servers support this feature!

另请注意,并非所有服务器都支持此功能!

回答by CodeBrauer

for checking multiple files it is better to use ftp_chdirinstead using the is_dir/ file_existsfunction with URL wrappers, cause there are pretty slow (foreach file there must be opened a new ftp connection ...).

为了检查多个文件,最好使用带有 URL 包装器ftp_chdiris_dir/file_exists函数,因为它非常慢(foreach 文件必须打开一个新的 ftp 连接......)。

foreach ($list as $item) {
    $is_dir = @ftp_chdir($ftp_handle, $item); //produces warning if file...
    if ($is_dir) {
        ftp_chdir($ftp_handle, '..');
        $dirs[] = $item;
    } else {
        $files[] = $item;
    }
}

This script was ready after ~10 sec.

该脚本在约 10 秒后准备就绪。

The same script using is_dirand file_existsneeded more than 45 sec. (each file had 10 runs)

使用相同的脚本is_dirfile_exists需要多于45秒。(每个文件有 10 次运行)

回答by vladkras

Me also not satisfied with any anwser incl. accepted one. I have standard connection (not O-O-style like in @Lackeeee's answer which is closest to mine):

我也不满意任何 anwser 包括。接受了一个。我有标准连接(不是像@Lackeeee 的答案中最接近我的那样的 OO 风格):

$ftp = ftp_connect($host, 21, 30);
ftp_login($ftp, $user, $password);

// if $my_dir name exists in array returned by nlist in current '.' dir
if (in_array($my_dir, ftp_nlist($ftp, '.'))) {
    // do smth with $my_dir
}

ftp_close($ftp);

回答by Szymon Sad?o

Here is a function to do this. It returns a simple trueor false. It also handles some edge cases.

这是执行此操作的函数。它返回一个简单的trueor false。它还处理一些边缘情况。

function isDir($connection, $dir)
{
    $dir = trim($dir, '/');
    $dir = '/' . $dir;
    if ($dir === '/') {
        return true;
    }
    return in_array($dir, ftp_nlist($connection, dirname($dir)));
}

回答by user2452922

ftp_nlist already kinda does it:

ftp_nlist 已经有点这样做了:

function ftp_is_dir($path) {
  global $ftp_conn;
  $list = ftp_nlist($ftp_conn, $path);
  if ($list && count($list)>1) return true;
  else return false;
}

ftp_nlist returns an array of one element when a valid file (not folder) is given as $path, whereas, even an empty folder will yield 2 elements.

当有效文件(不是文件夹)作为 $path 给出时,ftp_nlist 返回一个包含一个元素的数组,而即使是一个空文件夹也会产生 2 个元素。