/** * 根据URL下载文件到指定的文件目录 * * @param string $src 网络文件地址 * @param string $target 目标文件地址 * @return bool */ public static function downloadFile($src, $target, $cookie) { $ret = false; if (self::isWinOs()) { if (is_null(self::$streamContext)) { self::$streamContext = self::createStreamContext($cookie); } if (self::$streamContext === false) { return $ret; } if ($content = file_get_contents($src, false, self::$streamContext)) { $target = iconv('utf-8', 'gbk', $target); $ret = file_put_contents($target, $content); unset($content); } return $ret; } else { $agent = '--user-agent="Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0"'; $cookieParam = '--load-cookies=' . $cookie; exec('wget ' . $agent . ' ' . $cookieParam . ' -t 2 -T 5 "' . $src . '" -O "' . $target . '"', $out, $status); return file_exists($target) && filesize($target); } }
public function storageFile() { if (!is_array($this->lectureList)) { $this->error('列表为空'); } $this->_loadPreDownloaded(); foreach ($this->lectureList as $chapterKey => $chapterList) { $chapterDir = $this->_formatDir($chapterKey); foreach ($chapterList as $sectionKey => $sectionList) { $sectionDir = $this->_formatDir($sectionKey); foreach ($sectionList as $item) { $fileHash = md5($item); if (in_array($fileHash, array_keys($this->preDownloaded))) { continue; } $fileName = $this->_formatFileName($item); $dir = $this->lectureDir . '/' . $chapterDir . '/' . $sectionDir . '/'; LibStorage::mkdirs($dir); if (LibStorage::downloadFile($item, $dir . $fileName, $this->cookie)) { $this->_saveProgress($fileHash . "\t" . $dir . $fileName . PHP_EOL); echo '==>file(' . $dir . $fileName . ') DONE' . PHP_EOL; } } } } return $this; }