Как я могу проверить, существует ли файл на удаленном сервере с помощью PHP?

Вот некоторая информация о понижении приложений на Ubuntu.

http://www.howtogeek.com/howto/29584/safely-remove-ppas-and-roll-back-to-stable-versions-in-ubuntu/

gnome tweak tool

28
задан Felix Kling 31 January 2011 в 15:50
поделиться

3 ответа

Некоторые предложения:

51
ответ дан 28 November 2019 в 02:18
поделиться

Вы можете использовать ftp_nlist для просмотра списка всех файлов на удаленном сервере. Затем вам нужно выполнить поиск в массиве результатов, чтобы проверить, существует ли искомый файл.

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

1
ответ дан 28 November 2019 в 02:18
поделиться

Это оптимизация решения @JohanPretorius и ответ на комментарии о «медленном и неэффективном для больших каталогах» @Andrew и других: если вам требуется более одной «проверки file_exist», эта функция является оптимальным решением.

ftp_file_exists() кэширование последней папки

  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
ответ дан 28 November 2019 в 02:18
поделиться
Другие вопросы по тегам:

Похожие вопросы: