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

时间:2022-12-30 00:30:53

How can I check if a specific file exists on a remote server using PHP via FTP connections?

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

6 个解决方案

#1


30  

I used this, a bit easier:

我用这个,简单一点:

// 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);

Functions used: (thanks to middaparka)

所使用的函数:

  1. Login using ftp_connect

    登录使用作用

  2. Get the remote file list via ftp_nlist

    通过ftp_nlist获取远程文件列表

  3. Use in_array to see if the file was present in the array

    使用in_array查看该文件是否存在于该数组中

#2


48  

Some suggestions:

一些建议:

#3


7  

Just check the size of a file. If the size is -1, it doesn't exist, so:

检查文件的大小。如果大小是-1,它不存在,所以:

$fileSize = ftp_size($ftp_connection, "somefile.txt");

if ($fileSize != -1) {
    echo "File exists";
} else {
    echo "File does not exist";
}

If the size is 0, the file does exist, it is just 0 bytes.

如果大小为0,则文件确实存在,仅为0字节。

Source

#4


6  

This is an optimization of @JohanPretorius solution, and an answer for comments about "slow and inefficient for large dirs" of @Andrew and other: if you need more than one "file_exist checking", this function is a optimal solution.

这是对@JohanPretorius解决方案的优化,也是对@Andrew等人关于“对于大型dirs来说速度缓慢且效率低下”的评论的一个答案:如果您需要多个“file_exist check”,这个函数是一个最佳解决方案。

ftp_file_exists() caching last folder

  function ftp_file_exists(
      $file, // the file that you looking for
      $path = "SERVER_FOLDER",   // the remote folder where it is
      $ftp_server = "ftp.example.com", //Server to connect to
      $ftp_user = "ftpserver_username", //Server username
      $ftp_pwd = "ftpserver_password", //Server password
      $useCache = 1  // ALERT: do not $useCache when changing the remote folder $path.
  ){

      static $cache_ftp_nlist = array();
      static $cache_signature = '';

      $new_signature = "$ftp_server/$path";

      if(!$useCache || $new_signature!=$cache_signature) 
          {
              $useCache = 0;
              //$new_signature = $cache_signature;
              $cache_signature = $new_signature;
               // setup the connection
               $conn_id         = ftp_connect($ftp_server) or die("Error connecting $ftp_server");
               $ftp_login           = ftp_login($conn_id, $ftp_user, $ftp_pwd);
               $cache_ftp_nlist = ftp_nlist($conn_id, $path);

               if ($cache_ftp_nlist===FALSE)die("erro no ftp_nlist");
          }

        //$check_file_exist = "$path/$file";
        $check_file_exist = "$file";

        if(in_array($check_file_exist, $cache_ftp_nlist)) 
            {
                echo "Found: ".$check_file_exist." in folder: ".$path;
            } 
        else 
            {
                echo "Not Found: ".$check_file_exist." in folder: ".$path;  
            };
        // use for debuging: var_dump($cache_ftp_nlist);
        if(!$useCache) ftp_close($conn_id);
    } //function end

    //Output messages
    echo ftp_file_exists("file1-to-find.ext"); // do FTP
    echo ftp_file_exists("file2-to-find.ext"); // using cache
    echo ftp_file_exists("file3-to-find.ext"); // using cache
    echo ftp_file_exists("file-to-find.ext","OTHER_FOLDER"); // do FTP

#5


4  

A general solution would be to:

一般的解决办法是:

  1. Login using ftp_connect

    登录使用作用

  2. Navigate to the relevant directory via ftp_chdir

    通过ftp_chdir导航到相关目录

  3. Get the remote file list via ftp_nlist or ftp_rawlist

    通过ftp_nlist或ftp_rawlist获取远程文件列表

  4. Use in_array to see if the file was present in the array returned by ftp_rawlist

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

That said, you could potentially simply use file_exists if you have the relevant URL wrappers available. (See the PHP FTP and FTPS protocols and wrappers manual page for more information.)

也就是说,如果有相关的URL包装器可用,您可以使用file_exist。(更多信息请参见PHP FTP和FTPS协议和包装器手册页。)

#6


2  

You can use ftp_nlist to list all the files on the remote server. Then you should search into the result array to check if the file what you was looking for exists.

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

http://www.php.net/manual/en/function.ftp-nlist.php

http://www.php.net/manual/en/function.ftp-nlist.php

#1


30  

I used this, a bit easier:

我用这个,简单一点:

// 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);

Functions used: (thanks to middaparka)

所使用的函数:

  1. Login using ftp_connect

    登录使用作用

  2. Get the remote file list via ftp_nlist

    通过ftp_nlist获取远程文件列表

  3. Use in_array to see if the file was present in the array

    使用in_array查看该文件是否存在于该数组中

#2


48  

Some suggestions:

一些建议:

#3


7  

Just check the size of a file. If the size is -1, it doesn't exist, so:

检查文件的大小。如果大小是-1,它不存在,所以:

$fileSize = ftp_size($ftp_connection, "somefile.txt");

if ($fileSize != -1) {
    echo "File exists";
} else {
    echo "File does not exist";
}

If the size is 0, the file does exist, it is just 0 bytes.

如果大小为0,则文件确实存在,仅为0字节。

Source

#4


6  

This is an optimization of @JohanPretorius solution, and an answer for comments about "slow and inefficient for large dirs" of @Andrew and other: if you need more than one "file_exist checking", this function is a optimal solution.

这是对@JohanPretorius解决方案的优化,也是对@Andrew等人关于“对于大型dirs来说速度缓慢且效率低下”的评论的一个答案:如果您需要多个“file_exist check”,这个函数是一个最佳解决方案。

ftp_file_exists() caching last folder

  function ftp_file_exists(
      $file, // the file that you looking for
      $path = "SERVER_FOLDER",   // the remote folder where it is
      $ftp_server = "ftp.example.com", //Server to connect to
      $ftp_user = "ftpserver_username", //Server username
      $ftp_pwd = "ftpserver_password", //Server password
      $useCache = 1  // ALERT: do not $useCache when changing the remote folder $path.
  ){

      static $cache_ftp_nlist = array();
      static $cache_signature = '';

      $new_signature = "$ftp_server/$path";

      if(!$useCache || $new_signature!=$cache_signature) 
          {
              $useCache = 0;
              //$new_signature = $cache_signature;
              $cache_signature = $new_signature;
               // setup the connection
               $conn_id         = ftp_connect($ftp_server) or die("Error connecting $ftp_server");
               $ftp_login           = ftp_login($conn_id, $ftp_user, $ftp_pwd);
               $cache_ftp_nlist = ftp_nlist($conn_id, $path);

               if ($cache_ftp_nlist===FALSE)die("erro no ftp_nlist");
          }

        //$check_file_exist = "$path/$file";
        $check_file_exist = "$file";

        if(in_array($check_file_exist, $cache_ftp_nlist)) 
            {
                echo "Found: ".$check_file_exist." in folder: ".$path;
            } 
        else 
            {
                echo "Not Found: ".$check_file_exist." in folder: ".$path;  
            };
        // use for debuging: var_dump($cache_ftp_nlist);
        if(!$useCache) ftp_close($conn_id);
    } //function end

    //Output messages
    echo ftp_file_exists("file1-to-find.ext"); // do FTP
    echo ftp_file_exists("file2-to-find.ext"); // using cache
    echo ftp_file_exists("file3-to-find.ext"); // using cache
    echo ftp_file_exists("file-to-find.ext","OTHER_FOLDER"); // do FTP

#5


4  

A general solution would be to:

一般的解决办法是:

  1. Login using ftp_connect

    登录使用作用

  2. Navigate to the relevant directory via ftp_chdir

    通过ftp_chdir导航到相关目录

  3. Get the remote file list via ftp_nlist or ftp_rawlist

    通过ftp_nlist或ftp_rawlist获取远程文件列表

  4. Use in_array to see if the file was present in the array returned by ftp_rawlist

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

That said, you could potentially simply use file_exists if you have the relevant URL wrappers available. (See the PHP FTP and FTPS protocols and wrappers manual page for more information.)

也就是说,如果有相关的URL包装器可用,您可以使用file_exist。(更多信息请参见PHP FTP和FTPS协议和包装器手册页。)

#6


2  

You can use ftp_nlist to list all the files on the remote server. Then you should search into the result array to check if the file what you was looking for exists.

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

http://www.php.net/manual/en/function.ftp-nlist.php

http://www.php.net/manual/en/function.ftp-nlist.php