我想得到FTP文件的内容,而不保存在本地。
到目前为止,这就是我所拥有的:
$ftp_server = "my_server";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to the server");
if (@ftp_login($ftp_conn, "username", "password")) {
$local_file = 'C:\Users\user\Desktop\testing.txt';
$fp = fopen($local_file, "w");
$d = ftp_nb_fget($ftp_conn, $fp, "commands.yml", FTP_BINARY);
while ($d == FTP_MOREDATA) {
$d = ftp_nb_continue($ftp_conn);
}
if ($d != FTP_FINISHED) {
echo "Error downloading $server_file";
exit(1);
}
ftp_close($ftp_conn);
fclose($fp);
$filename = 'C:\Users\user\Desktop\testing.txt';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
} else {
echo "Couldn't establish a connection.";
}上面的代码保存文件并读取文件内容。是否可以在不保存本地文件的情况下读取该文件?
发布于 2014-06-04 00:36:03
从官方PHP站点上的答案到bob at notallhere dot com
不想使用中间文件?使用'php:// output‘作为文件名,然后使用输出缓冲捕获输出。
ob_start();
$result = ftp_get($ftp, "php://output", $file, FTP_BINARY);
$data = ob_get_contents();
ob_end_clean();https://stackoverflow.com/questions/24027171
复制相似问题