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

后端 未结 6 1248
时光取名叫无心
时光取名叫无心 2020-12-02 20:47

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

6条回答
  •  温柔的废话
    2020-12-02 20:59

    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

提交回复
热议问题