PHP远程下载文件

<?php
$file_path = 'example.txt';
$min_size = 100;

if (!file_exists($file_path) || filesize($file_path) < $min_size) {
  // 如果文件不存在或者小于100字节,则从远程访问html页面
  $html_url = 'https://www.example.com/example.html';
  $html_content = file_get_contents($html_url);
  // 提取需要的内容,可以使用正则表达式或其他方法进行匹配
  $txt_content = preg_replace('/<[^>]+>/', '', $html_content); // 去除HTML标签
  // 保存为txt文件
  file_put_contents($file_path, $txt_content);
}

标签

发表评论