Imoen Posted November 15, 2008 Share Posted November 15, 2008 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>'); ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted November 15, 2008 Share Posted November 15, 2008 If you have zlib installed/enabled, it's pretty easy. http://php.net/zlib Quote Link to comment Share on other sites More sharing options...
Imoen Posted November 15, 2008 Author Share Posted November 15, 2008 I believe it is installed because I can manually extract the files just fine. I am just unsure what "command" means to extract the file. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 15, 2008 Share Posted November 15, 2008 http://php.net/manual/en/function.gzfile.php Would probably work fine for you. Quote Link to comment Share on other sites More sharing options...
Imoen Posted November 15, 2008 Author Share Posted November 15, 2008 This doesn't seem to extract the test files, as after I run the php there are no txt files in place of the gz files. I'm not sure what I'm doing wrong? Quote Link to comment Share on other sites More sharing options...
Imoen Posted November 16, 2008 Author Share Posted November 16, 2008 No one knows? Quote Link to comment Share on other sites More sharing options...
corbin Posted November 16, 2008 Share Posted November 16, 2008 That just opens the file in a workable file handle. It would be equivalent to doing: $f = fopen('somefile.gz', 'r'); DoSomeVooDooDecompressingMagic($f); while(!feof($f)) { echo fread($f, 1024); } Quote Link to comment Share on other sites More sharing options...
Imoen Posted November 16, 2008 Author Share Posted November 16, 2008 I need it to extract the file, not just open it. Isn't there a way to do that? Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 16, 2008 Share Posted November 16, 2008 exec('unzip my_zip_file.zip'); chmod -R 755 * Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.