Jump to content

Script not working as expected.


unkwntech

Recommended Posts

I wrote this script but it is not working as expected, If should read http://pubredirect.servercommand.org/cod4/usermaps/ then for each folder it should get each file inside those folders and download it to a local directory.

However each file in the directories is being written as the same file, so I end up with 3 files of the same size and content.

<?php
ini_set('allow_url_fopen', '1');
$root = 'pubredirect.servercommand.org/cod4/usermaps/';
$localFolder = 'C:\Program Files\Activision\Call of Duty 4 - Modern Warfare\usermaps';
$f = file('http://' . $root);
$f = preg_replace('/\<br\>/i', "\n", $f['2']);
preg_match_all('%/cod4/usermaps/.*?/%i', $f, $dirs);
$numFiles = count($dirs['0']);
echo "There are " . $numFiles . " maps to download.\nThis will take some time.\n\nThis was built for ->TYL<-\n\t\thttp://tyl.clanservers.com\n";
$curFile = 1;
foreach($dirs['0'] as $folder)
{
	echo "Now downloading map " . $curFile . "/" . $numFiles . " Map Name: ";
  	$folder = preg_replace('%/cod4/usermaps/%', '', $folder);
	$folder = preg_replace('%/%', '', $folder);

$remoteRoot = $root . $folder . '/';
$subF = file('http://' . $remoteRoot);
$subF = preg_replace('/></i', ">\n<", $subF['2']);
$subF = explode("\n", $subF);
foreach($subF as $line)
 {
	if(preg_match('/<br>\s*?[a-z]{6,9},\s[a-z]{3,9}\s[0-9]{2},\s200[0-9]{1}\s*?[0-9]{1,2}:[0-9]{2}\s*?(?:a|p)m\s*?[0-9]*?\s*?<a href="/i', $line))
	 {
		$line = preg_replace('/<br>\s*?[a-z]{6,9},\s[a-z]{3,9}\s[0-9]{2},\s200[0-9]{1}\s*?[0-9]{1,2}:[0-9]{2}\s*?(?:a|p)m\s*?[0-9]*?\s*?<a href="/i', '', $line) . "\n";
		$line = preg_replace('%">.*?</a>%i', '', $line);
		$files[] = $line;
	 }
 }
$i=0;
foreach($files as $file)
 {
	$gets[$i]['filename'] = preg_replace('%/cod4/usermaps/.*?/%i', '', $file);
	$gets[$i]['filename'] =  preg_replace('/\n/', '', $gets[$i]['filename']);
	$mapName = preg_replace('/\..{2,3}/', '', $gets[$i]['filename']);
	echo $mapName . "\n";
	$noPath = preg_replace('%/cod4/usermaps/%i', '', $file);
	$noPath = preg_replace('%/%', '\\', $noPath);
	$gets[$i]['localDir'] = $localFolder . '\\' . preg_replace('/[a-z0-9_]*?\..{2,3}/i', '', $noPath);
	$gets[$i]['localDir'] =  preg_replace('/\n/', '', $gets[$i]['localDir']);
	$gets[$i]['remoteDir'] = $root . preg_replace('%/cod4/usermaps/%i', '', preg_replace('/[a-z0-9_]*?\..{2,3}/i', '', $file));
	$gets[$i]['remoteDir'] = preg_replace('%//%i', '/', $gets[$i]['remoteDir']);
	$gets[$i]['remoteDir'] = preg_replace('/\n/', '', $gets[$i]['remoteDir']);
	$i++;
 }
foreach($gets as $map)
{
	if(!file_exists($map['localDir']))
	 {
	 	$command = "mkdir " . '"' . $map['localDir'] . '"';
	 	passthru($command, $r);
	 }
 	$fileToGet = 'http://' . $map['remoteDir'] . $map['filename'];
	$remoteFH = fopen($fileToGet, 'r');
	while(!FEOF($remoteFH))
	 {
		$data .= fgets($remoteFH);
	 }
	fclose($remoteFH);
	$localFile = $map['localDir'] . '\\' . $map['filename'];
	$localFH = fopen($localFile, 'w');
	fwrite($localFH, $data);
	fclose($localFH);
	 }
	$curFile++;
	 }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.