Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP检查远程服务器上是否存在文件?_Php - Fatal编程技术网

如何使用PHP检查远程服务器上是否存在文件?

如何使用PHP检查远程服务器上是否存在文件?,php,Php,如何通过FTP连接使用PHP检查远程服务器上是否存在特定文件?一般解决方案是: 使用登录 通过导航到相关目录 通过或获取远程文件列表 用于查看ftp_rawlist返回的数组中是否存在该文件 也就是说,如果您有相关的URL包装器可用,那么您可能只需使用。(有关更多信息,请参阅。)一些建议: 使用ftp\u size,如果不存在,则返回-1: 使用fopen,例如fopen(“,“r”) 使用ftp\u nlist,检查所需文件名是否在列表中: 您可以使用ftp列表列出远程服务器上的所有文件。然

如何通过FTP连接使用PHP检查远程服务器上是否存在特定文件?

一般解决方案是:

  • 使用登录

  • 通过导航到相关目录

  • 通过或获取远程文件列表

  • 用于查看ftp_rawlist返回的数组中是否存在该文件

  • 也就是说,如果您有相关的URL包装器可用,那么您可能只需使用。(有关更多信息,请参阅。)

    一些建议:

    • 使用
      ftp\u size
      ,如果不存在,则返回-1:
    • 使用
      fopen
      ,例如fopen(“,“r”)
    • 使用
      ftp\u nlist
      ,检查所需文件名是否在列表中:

    您可以使用ftp列表列出远程服务器上的所有文件。然后,您应该搜索结果数组,以检查您要查找的文件是否存在


    我用了这个,简单一点:

    // the server you wish to connect to - you can also use the server ip ex. 107.23.17.20
            $ftp_server = "ftp.example.com";
    
    // set up a connection to the server we chose or die and show an error
            $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
            ftp_login($conn_id,"ftpserver_username","ftpserver_password");
    
    // check if a file exist
            $path = "/SERVER_FOLDER/"; //the path where the file is located
    
            $file = "file.html"; //the file you are looking for
    
            $check_file_exist = $path.$file; //combine string for easy use
    
            $contents_on_server = ftp_nlist($conn_id, $path); //Returns an array of filenames from the specified directory on success or FALSE on error. 
    
    // Test if file is in the ftp_nlist array
            if (in_array($check_file_exist, $contents_on_server)) 
            {
                echo "<br>";
                echo "I found ".$check_file_exist." in directory : ".$path;
            }
            else
            {
                echo "<br>";
                echo $check_file_exist." not found in directory : ".$path;  
            };
    
            // output $contents_on_server, shows all the files it found, helps for debugging, you can use print_r() as well
            var_dump($contents_on_server);
    
    // remember to always close your ftp connection
            ftp_close($conn_id);
    
    //您希望连接的服务器-您也可以使用服务器ip,例如107.23.17.20
    $ftp\u server=“ftp.example.com”;
    //设置与我们选择的服务器的连接,否则将显示错误
    $conn_id=ftp_connect($ftp_server)或die(“无法连接到$ftp_server”);
    ftp_登录($conn_id,“ftpserver_用户名”,“ftpserver_密码”);
    //检查文件是否存在
    $path=“/SERVER_FOLDER/”//文件所在的路径
    $file=“file.html”//您正在查找的文件
    $check\u file\u exist=$path.$file//组合字符串以便于使用
    $contents\u on\u server=ftp\u nlist($conn\u id,$path)//成功返回指定目录中的文件名数组,错误返回FALSE。
    //测试文件是否在ftp列表数组中
    if(在数组中($check_file_exist,$contents_on_server))
    {
    回声“
    ”; 在目录:“.$path”中回显“我找到了”。$check\u file\u exist.”; } 其他的 { 回声“
    ”; echo$check_file_exist.。未在目录中找到:“.$path; }; //在\u服务器上输出$contents\u,显示找到的所有文件,有助于调试,还可以使用print\u r() 变量转储(服务器上的内容); //请记住始终关闭ftp连接 ftp_关闭($conn_id);
    使用的函数:(感谢middaparka)

  • 使用登录

  • 通过获取远程文件列表

  • 用于查看阵列中是否存在该文件


  • 这是对@JohanPretorius解决方案的优化,也是对@Andrew和other关于“大型dirs的速度慢且效率低”的评论的回答:如果您需要多个“文件存在检查”,此函数是最佳解决方案

    ftp\u文件存在()
    缓存最后一个文件夹
    只需检查文件的大小。如果大小为
    -1
    ,则它不存在,因此:

    $file_size = ftp_size($ftp_connection, "example.txt");
    
    if ($file_size != -1) {
        echo "File exists";
    
    } else {
        echo "File does not exist";
    }
    
    如果大小为
    0
    ,则文件确实存在,仅为0字节


    如果有一个大文件列表(5000+以上),这可能会很慢,而且对于大目录来说,速度慢且效率低,与previous@Andrew包括我的
    文件\u存在
    建议,或者你只是在执行一项微不足道的向下投票任务?不确定这些评论。操纵5000数组还是制作5000<代码>ftp_size()?使用此解决方案进行测试,并将其与单个网络调用进行比较(
    ft\u size()
    等)。取决于延迟、网络拥塞等因素。php7.3+阵列现在速度更快。但是,不要在数组()中执行
    ,而是执行
    数组翻转(ftp\u nlist(…)
    ,然后检查是否存在
    isset()
    。快多了
    fopen()
    file\u exits()
    非常优雅,但要避免“误报”。关于
    ftp\u nlist
    和@Andrew comment,我在这里加入了一个“缓存解决方案”作为一个以上的答案。关于已评论的问题:se。如果您有数十万个或更多文件怎么办?下载所有文件的列表不是最好的选择。@kintsukuroi如果你有那么多文件,你可以做一些事情-1。向下钻取文件夹并搜索每个文件夹。2.缓存文件夹中的文件列表并使用该列表。3.如果您托管在unix设备上,则可以执行bash命令
    ls
    ,并将其写入json文件并搜索。4.这可能不是您的解决方案,这是几年前编写的,用于在小站点中搜索根目录中的特定文件。@drmzindec1.-所有文件都在一个文件夹中。2.-最佳解决方案是ftp_大小。3.-谢谢你抽出时间,祝你好运。对不起,这是个老问题,但人们还是来这里寻求答案。:)+问题1!对此要小心。在(某些?)ftp服务器上,目录名上的ftp_size()也返回-1(无论它是否存在)。我理解这意味着你只能在一个文件上运行它,但是值得注意的是这个限制。
    $file_size = ftp_size($ftp_connection, "example.txt");
    
    if ($file_size != -1) {
        echo "File exists";
    
    } else {
        echo "File does not exist";
    }