Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a file exists on a remote server using PHP?

Tags:

php

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

like image 340
A.B.Developer Avatar asked Jan 31 '11 15:01

A.B.Developer


People also ask

How can check file exist in PHP server?

The file_exists() function in PHP is an inbuilt function which is used to check whether a file or directory exists or not. The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns True on success and False on failure.

How do you check if a file exists from a URL?

get_headers() will return an array with the headers from an HTTP request. The first index has the HTTP response code, HTTP code 200 means the request was a success (file exists). You will get a 404 code if nothing exists for the request. If the “200 OK” is found then true is returned else false is returned.

How can I tell if a text file exists?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .

How to check if a file exists from a URL in PHP?

Today we’ll explain to you how to check if a file exists from a URL in PHP. Using php built-in file_exists () function, we can check whether a file or directory exists on the server or not but will not use it to check if a file exists on a remote server or not.

How to check if a file exists on a remote server?

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. The code has been written by: @Drmzindec should be change a little:

How do I check if a file exists in a directory?

Code language: PHP (php) The file_exists () function accepts a filename and returns true if the file exists; otherwise it returns false. The following example uses the file_exists () function to check if the file readme.txt exists in the current directory:

How to check if a path is a file in PHP?

If you want to check if a path is a file (not a directory) and exists in the file system, you can use the is_file () function. The is_file () function accepts a $filename and returns true if the $filename is a file and exists: is_file (string $filename) : bool Code language: PHP (php)


6 Answers

Some suggestions:

  • Use ftp_size, which returns -1 if it doesn't exist: http://www.php.net/manual/en/function.ftp-size.php
  • Use fopen, e.g. fopen("ftp://user:[email protected]/somefile.txt", "r")
  • Use ftp_nlist, check to see if the filename you want is in the list: http://www.php.net/manual/en/function.ftp-nlist.php
like image 126
Merijn Avatar answered Oct 14 '22 00:10

Merijn


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

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

like image 29
Drmzindec Avatar answered Oct 13 '22 23:10

Drmzindec


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

$file_size = ftp_size($ftp_connection, "example.txt");

if ($file_size != -1) {
    echo "File exists";

} else {
    echo "File does not exist";
}

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

Source

like image 41
emotality Avatar answered Oct 14 '22 01:10

emotality


A general solution would be to:

  1. Login using ftp_connect

  2. Navigate to the relevant directory via ftp_chdir

  3. Get the remote file list via ftp_nlist or ftp_rawlist

  4. Use in_array to see if the file was present in the array returned by 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.)

like image 21
John Parker Avatar answered Oct 14 '22 00:10

John Parker


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.

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
like image 20
4 revs, 3 users 67% Avatar answered Oct 14 '22 00:10

4 revs, 3 users 67%


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.

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

like image 29
Fran Verona Avatar answered Oct 13 '22 23:10

Fran Verona