Jump to content

Need help to extract .gz files on server


Imoen

Recommended Posts

I have a cron job that will download files named ally.txt.gz, tribe.txt.gz, village.txt.gz, kill_att.txt.gz, kill_def.txt.gz and kill_all.txt.gz from another server onto my server. Inside each of these gz files are csv txt files to be used to update my database. The import calls for the txt files to be used, so what I need to do is to extract the gz files. This update happens hourly so I need this to be able to be added to the php file that downloads these files so they are there when the import.php file runs. The gz files are downloaded to /public_html/jellybabiesgang/input and I would like the extracted files to be in the same location.

 

This is the download code:

<?php

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_staemme.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);

$user->add_lang("install");
$user->add_lang("acp/staemme");

$domain = get_serverdomain();
$folder = $phpbb_root_path . $staemme_config['import_path'];
$files = get_worlddata_files();
$fileEx = "txt.gz";
$remote_dir = '/map';

$allowed = ini_get("allow_url_fopen");
$s_time = microtime(TRUE);

//
// Preconditions - Write Permissions in input folder
//
$dir = $staemme_config['import_path'];
$exists = $write = false;

// Try to create the directory if it does not exist
if (!file_exists($phpbb_root_path . $dir))
{
@mkdir($phpbb_root_path . $dir, 0777);
@chmod($phpbb_root_path . $dir, 0777);
}

// Now really check
if (file_exists($phpbb_root_path . $dir) && is_dir($phpbb_root_path . $dir))
{
if (!@is_writable($phpbb_root_path . $dir))
{
	@chmod($phpbb_root_path . $dir, 0777);
}
$exists = true;
}

// Now check if it is writable by storing a simple file
$fp = @fopen($phpbb_root_path . $dir . 'test_lock', 'wb');
if ($fp !== false)
{
$write = true;
}
@fclose($fp);

@unlink($phpbb_root_path . $dir . 'test_lock');

$exist_msg = ($exists) ? '<strong style="color:green">' . $user->lang['FOUND'] . '</strong>' : '<strong style="color:red">' . $user->lang['NOT_FOUND'] . '</strong>';
$write_msg = ($write) ? ', <strong style="color:green">' . $user->lang['WRITABLE'] . '</strong>' : (($exists) ? ', <strong style="color:red">' . $user->lang['UNWRITABLE'] . '</strong>' : '');

//
//Function-Part
//

$skip		= isset($_REQUEST['skip']) ? request_var('skip', 0) : FALSE;
$override	= isset($_REQUEST['override']) ? request_var('override', '') : FALSE;
$timeouts	= isset($_REQUEST['timeouts']) ? request_var('timeouts', 0) : 0;

echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head><script type="text/javascript">
/*
function checkready () {
var Ausdruck = /(LOCK\=LD)/;
	if(Ausdruck.exec(document.body.innerHTML) != null) msgbox("LD");
	var Ausdruck = /(LOCK\=LI)/;
	if(Ausdruck.exec(document.body.innerHTML) != null) msgbox("LI");
	var Ausdruck = /(LOCK\=LG)/;
	if(Ausdruck.exec(document.body.innerHTML) != null) msgbox("LG");
	var Ausdruck = /FILEID=([0-9]+)/g;
	var matches = document.body.innerHTML.match(Ausdruck);
	var file_id;
	if(matches != null)
	{
		matches = matches[matches.length - 1].match(/FILEID=([0-9]+)/);
		file_id = matches[1];
	}
	var Ausdruck = /(Maximum execution time)/g;
	if(Ausdruck.exec(document.body.innerHTML) != null && file_id != null)
	{
		redir(file_id);
	};
	aktiv = window.setTimeout("checkready()", 4000);
}
*/
function msgbox(id)
{
var text_var;
if(id == "LD") text_var = "'.$user->lang['JS_DELETE_IN_PROGRESS'].'";
else if(id == "LI") text_var = "'.$user->lang['JS_IMPORT_IN_PROGRESS'].'";
else if(id == "LG") text_var = "'.$user->lang['JS_DOWNLOAD_IN_PROGRESS'].'";
var box=window.confirm(text_var);
if(box==true)
{
	window.location = "download.php?override=" + id;
}
else if(box==false)
{

}
} 

function stop () {
  window.clearTimeout(aktiv);
}

function redir (target)
{
window.location = "download.php?skip=" + target'.(($timeouts != 0) ? ' + "&timeouts='.$timeouts.'"' : "").';
}

function longtimeout()
{
window.location = "download.php?skip=" + target'.(($timeouts != 0) ? ' + "&timeouts='.$timeouts.'"' : "").';
}

var aktiv = window.setTimeout("checkready()", 4000);
var aktiv2 = window.setTimeout("longtimeout()", 5*60000);
</script>
<body>';

msg($user->lang['CHECKING_PRECONDITIONS']);
msg(sprintf($user->lang['DOWNLOAD_METHOD'], $dir).": ".($allowed ? $user->lang['METHOD_FOPEN_COPY'] : $user->lang['METHOD_FSOCKOPEN_COPY']));
msg(sprintf($user->lang['INPUT_FOLDER'], $dir).": ".$exist_msg.$write_msg);
if(!$exists || !$write)
{
msg($user->lang['PRECONDITIONS_FAILED']);
msg(sprintf($user->lang['MAKE_FOLDER_WRITABLE'], $dir));
die;
}
else
{
msg($user->lang['PRECONDITIONS_SUCCEEDED']);
}

if($skip === FALSE)
{
//delWorld.lock
if(file_exists($folder . 'delWorld.lock') && $override != 'LD') die($user->lang['DELETE_IN_PROGRESS'].'<input type="hidden" name="lock" value="(LOCK=LD)" /></body></html>');

//importWorld.lock
if(file_exists($folder . 'importWorld.lock') && $override != 'LI') die($user->lang['IMPORT_IN_PROGRESS'].'<input type="hidden" name="lock" value="(LOCK=LI)" /></body></html>');

//getWorld.lock
if(file_exists($folder . 'getWorld.lock') && $override != 'LG') die($user->lang['DOWNLOAD_IN_PROGRESS'].'<input type="hidden" name="lock" value="(LOCK=LG)" /></body></html>');
add_lock($folder, "get");
}

function cb()
{
$max_time = ini_get('max_execution_time');
msg(sprintf($user->lang['TIMELIMIT_REACHED'], $max_time, $max_time).'</center><p>');
@set_time_limit($max_time);
}

function fsockCopy($url, $localFolder, $fileName)
{
global $user;

$f_time = microtime();
$fullFileName = $localFolder.$fileName;
msg('<center>'.sprintf($user->lang['CREATE_FILE'], $fullFileName));
$file = fopen($fullFileName, 'w');
if(!$file)
{
	msg($user->lang['CREATE_FAILED'].'</center>', TRUE, FALSE);
	return FALSE;
}
else
{
	msg($user->lang['CREATE_SUCCESS'].'</center>', TRUE, FALSE);
}
msg('<center>'.sprintf($user->lang['DOWNLOAD_FILE'], $url));

$data = fsockget($url, FALSE, "cb");
if(empty($data))
{
	msg('</center><center>'.$user->lang['FSOCKOPEN_DISABLED'].'<input type="hidden" name="method" value="2"></center>');
	$data = fcurlGet($url);
}	
if(empty($data))
{
	msg(sprintf($user->lang['DOWNLOAD_FAILED'], round(microtime(TRUE) - $f_time, 2)).'</center>', TRUE, FALSE);
	return FALSE;
}
fwrite($file, $data);
fclose($file);
msg(sprintf($user->lang['DOWNLOAD_SUCCESS'], round(microtime(TRUE) - $f_time, 2)).'</center>', TRUE, FALSE);
return TRUE;
}

if(!$allowed)
{
msg('<input type="hidden" name="method" value="1">');
foreach($files as $id => $file_name)
{
	if($id === $skip)
	{
		msg('<center>'.sprintf($user->lang['SKIP_FILE'], "$file_name.$fileEx").'</center>', TRUE, FALSE);
		continue;
	}
	$url = $domain . '/map/'."$file_name.$fileEx";
	fsockCopy($url, $folder, $file_name);
	@flush();
	if(function_exists("ob_flush")) @ob_flush();
}
}
else
{
msg('<input type="hidden" name="method" value="0">');
foreach($files as $id => $file_name)
{
	if(is_numeric($skip) && $id <= $skip)
	{
		msg('<center>'.sprintf($user->lang['SKIP_FILE'], "$file_name.$fileEx").'</center>', TRUE, FALSE);
		continue;
	}
	$f_time = microtime(TRUE);
	$url = $domain . '/map/'."$file_name.$fileEx";
	if(file_exists($folder . '/'."$file_name.$fileEx"))
	{
		unlink($folder ."$file_name.$fileEx");
	}
	msg('<center>'.sprintf($user->lang['DOWNLOAD_FILE'], $url).'<input type="hidden" name="file" value="FILEID='.$id.'">');		
	$result = copy($url, $folder . '/'."$file_name.$fileEx");
	if($result) msg(sprintf($user->lang['DOWNLOAD_SUCCESS'], round(microtime(TRUE) - $f_time, 2)).'</center>', TRUE, FALSE);
	else msg(sprintf($user->lang['DOWNLOAD_FAILED'], round(microtime(TRUE) - $f_time, 2)).'</center>', TRUE, FALSE);
}
}

set_staemme_config('download_date', time());
remove_lock($folder, "get");

msg('<center>'.sprintf($user->lang['DOWNLOAD_FINISHED'], ini_get('max_execution_time') * $timeouts + round(microtime(TRUE) - $s_time, 2)).'</center>', FALSE, FALSE);
msg('</body><html>');
?>  

 

Link to comment
https://forums.phpfreaks.com/topic/132832-need-help-to-extract-gz-files-on-server/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.