如何使用 PHP 检查远程服务器上是否存在文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4852767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if a file exists on a remote server using PHP?
提问by A.B.Developer
How can I check if a specific file exists on a remote server using PHP via FTP connections?
如何通过 FTP 连接使用 PHP 检查远程服务器上是否存在特定文件?
回答by Merijn
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
- 使用
ftp_size
,如果不存在则返回 -1:http: //www.php.net/manual/en/function.ftp-size.php - 使用
fopen
,例如 fopen(" ftp://user:[email protected]/somefile.txt", "r") - 使用
ftp_nlist
,检查您想要的文件名是否在列表中:http: //www.php.net/manual/en/function.ftp-nlist.php
回答by Drmzindec
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)
使用的功能:(感谢 middaparka)
Login using ftp_connect
Get the remote file list via ftp_nlist
Use in_arrayto see if the file was present in the array
使用ftp_connect登录
通过ftp_nlist获取远程文件列表
使用in_array查看文件是否存在于数组中
回答by emotality
Just check the size of a file. If the size is -1
, it doesn't exist, so:
只需检查文件的大小。如果大小为-1
,则它不存在,因此:
$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.
如果大小为0
,则该文件确实存在,只有 0 个字节。
回答by Peter Krauss
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 和其他人关于“大目录缓慢且效率低下”的评论的回答:如果您需要多个“file_exist 检查”,则此功能是最佳解决方案。
ftp_file_exists()
caching last folder
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
回答by John Parker
A general solution would be to:
一般的解决方案是:
Login using ftp_connect
Navigate to the relevant directory via ftp_chdir
Get the remote file list via ftp_nlistor ftp_rawlist
Use in_arrayto see if the file was present in the array returned by ftp_rawlist
使用ftp_connect登录
通过ftp_chdir导航到相关目录
通过ftp_nlist或ftp_rawlist获取远程文件列表
使用in_array查看文件是否存在于 ftp_rawlist 返回的数组中
That said, you could potentially simply use file_existsif you have the relevant URL wrappers available. (See the PHP FTP and FTPS protocols and wrappers manual pagefor more information.)
也就是说,如果您有相关的 URL 包装器可用,您可以简单地使用file_exists。(有关更多信息,请参阅PHP FTP 和 FTPS 协议和包装器手册页。)
回答by Fran Verona
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 列出远程服务器上的所有文件。然后您应该搜索结果数组以检查您要查找的文件是否存在。