脚本宝典收集整理的这篇文章主要介绍了php – 使用一个连接读取FTP目录中每个文件的内容,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这就是我所拥有的:
// set up basic connection $conn_id = ftp_connect('HOST_ADDRESS'); // lo@R_406_2848@ wITh username and password $LOGin_result = ftp_login($conn_id,'USERNAME','PASSWORD'); if (!$login_result) { exit(); } // get contents of the current directory $contents = ftp_nlist($conn_id,"DirectoryName"); $files = []; foreach ($contents AS $content) { $ignoreArray = ['.','..']; if ( ! in_array( $content,$ignoreArray) ) { $files[] = $content; } }
上面的工作很好,以获取我需要从中获取内容的文件名.接下来,我想通过文件名数组进行递归,并将内容存储到变量中以便进一步处理.
foreach ($files AS $file ) { $handle = foPEn($filename,"r"); $contents = fread($conn_id,filesize($file)); $content[$file] = $contents; }
上面的想法来自这里:
PHP: How do I read a .txt file from FTP server into a variable?
ftp_get
并重用您的连接ID($conn_id):
foreach ($files as $file) { // Full path to a remote file $remote_path = "DirectoryName/$file"; // Path to a temporary local copy of the remote file $temp_path = tempnam(Sys_get_temp_dir(),"ftp"); // Temporarily download the file ftp_get($conn_id,$temp_path,$remote_path,FTP_BINARY); // Read the contents of temporary copy $contents = file_get_contents($temp_path); $content[$file] = $contents; // Discard the temporary copy unlink($temp_path); }
以上是脚本宝典为你收集整理的php – 使用一个连接读取FTP目录中每个文件的内容全部内容,希望文章能够帮你解决php – 使用一个连接读取FTP目录中每个文件的内容所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。