Jump to content

Recommended Posts

Hello i was wondering what the best way to code the following script would be.. i have tried the code in the following box:

if(isset($_POST['update_system'])){
$filetype  = $_FILES['update_file']['type'];
$filename  = $_FILES['update_file']['name'];
$tempname  = $_FILES['update_file']['tmp_name'];
$filename_exp = explode('.',$filename);
$valext = $filename_exp[1];
$val_explode = explode('_',$filename_exp[0]);
$val1 = $val_explode[0];
$val2 = $val_explode[1];
$date = $val_explode[2];
$date_day = substr($date, 6,2);
$date_month = substr($date, 4,2);
$date_year = substr($date, 0,4);


if($filetype == 'application/octet-stream' && $valext == 'rar' && $val1 == 'addable' && $val2 == 'update'){
$prev_location = 'system_updates/previous/'.$filename;
$curr_location = 'system_updates/current/'.$filename;
$worar = explode('.',$filename);
$worar2 = $worar[0];
$output = 'system_updates/current';
$output2 = 'system_updates/previous';


/* EMPTY OLD */
$prescanH = scandir($output2);
foreach($prescanH as $preH){
if($preH != '.' && $preH != '..'){
unlink($output2.'/'.$preH);
}
}


/* MOVE CURRENT TO OLD */ 
$prescan = scandir($output);
foreach($prescan as $pre){
if($pre != '.' && $pre != '..'){
system('mv '.$output.'/'.$pre.' '.$output2.'/'.$pre.'');
}
}
$prescana = scandir($output);
foreach($prescana as $prea){
if($prea != '.' && $prea != '..'){
unlink($output.'/'.$prea);
}
}
move_uploaded_file($tempname, $curr_location);


/* UNPACK RAR & COPY FROM TEMP */
$rar_file = rar_open($curr_location);
$list = rar_list($rar_file);


foreach($list as $file) {
$entry = rar_entry_get($rar_file, $file->getName());
$entry -> extract($output);
}
rar_close($rar_file);


$rrmdir_arr = array('content','example_content');
foreach($rrmdir_arr as $rrmdirs){
rrmdir($rrmdirs);
system('mv '.$output.'/'.$worar2.'/'.$rrmdirs.' .');
}


} else {
$error_message = 'Error Uploading File..';
echo "<script> window.location.href='".$_SERVER['PHP_SELF']."?page=".$_GET['page']."&error=1&error_message=".$error_message."'; </script>";
}
}

 

LIBRARY SETUP

 

index.php

 

--- CONTENT

 

--- SYSTEM_UPDATES

     --- CURRENT

     --- PREVIOUS

 

What it does is takes the uploaded file - checks its a rar extension.

then deletes everything from PREVIOUS and moves rar from CURRENT to PREVIOUS.

 

It then unzips the current into the current directory then removes everything on the base directory except for the SYSTEM_UPDATES folder.

 

Then it moves the files from the folder in the CURRENT DIRECTORY out and replaces onto the root of website.

 

I am not happy about this but scandir still seemed to pick up the files inside the SYSTEM_UPDATES folder: 

 

$rrmdir_arr = array('content','example_content');

foreach($rrmdir_arr as $rrmdirs){
rrmdir($rrmdirs);
system('mv '.$output.'/'.$worar2.'/'.$rrmdirs.' .');
}

 

 

The weird thing is.. it works fine first time. As soon as i try to upload another to do the update - it all goes weird, and seems to skip the part where it copies all the files out - yet it stil deletes the directory...

or the rar uncompression doesnt work.

 

Thanks

Lewis

Edited by Ch0cu3r
fixed code tags
Link to comment
https://forums.phpfreaks.com/topic/290456-php-help/
Share on other sites

Since you have an invalid function call here (rrmdir)  I'm guessing that you didn't bother to turn on php error checking.  Do So Now.  See what other errors you may have.

 

You could also review this and perhaps shorten it up a bit.  For example - why do you explode $filename twice?  Why do you not assign the path names up front and use them in place of literal paths being used twice?

Link to comment
https://forums.phpfreaks.com/topic/290456-php-help/#findComment-1487799
Share on other sites

 

 

nce you have an invalid function call here (rrmdir)  I'm guessing that you didn't bother to turn on php error checking.  Do So Now.  See what other errors you may have.

 

Forgot to include this function in the post:

function rrmdir($dir) { 
   if (is_dir($dir)) { 
	 $objects = scandir($dir); 
	 foreach ($objects as $object) { 
	   if ($object != "." && $object != "..") { 
		 if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); 
	   } 
	 } 
	 reset($objects); 
	 rmdir($dir); 
   } 
}

Although i just get error like:

[14-Aug-2014 21:12:37 UTC] PHP Warning:  unlink(system_updates/previous/addable_update_20140813): Is a directory in /home/addable/public_html/system/content/page_content/admin/admin_system_update.php on line 28
 
- Even though it worked fine the first time...
Link to comment
https://forums.phpfreaks.com/topic/290456-php-help/#findComment-1487834
Share on other sites

I tidied it up abit:

	if(isset($_POST['update_system'])){
		$filetype 	= $_FILES['update_file']['type'];
		$filename 	= $_FILES['update_file']['name'];
		$tempname 	= $_FILES['update_file']['tmp_name'];
		$filename_exp	= explode('.',$filename);
		$filename2	= $filename_exp[0];
		$valext		= $filename_exp[1];
		$val_explode	= explode('_',$filename_exp[0]);
		$val1		= $val_explode[0];
		$val2		= $val_explode[1];

		if($filetype == 'application/octet-stream' && $valext == 'rar' && $val1 == 'addable' && $val2 == 'update'){
			$output = 'system_updates/current';
			$output2 = 'system_updates/previous';
			
			/* EMPTY OLD */
			$prescanH = scandir($output2);
			foreach($prescanH as $preH){
				if($preH != '.' && $preH != '..'){
					unlink($output2.'/'.$preH);
				}
			}
			
			/* MOVE CURRENT TO OLD */			
			$prescan = scandir($output);
			foreach($prescan as $pre){
				if($pre != '.' && $pre != '..'){
					system('mv '.$output.'/'.$pre.' '.$output2.'/'.$pre.'');
					unlink($output.'/'.$prea);
				}
			}
			
			/* MOVE FROM TEMP */
			move_uploaded_file($tempname, $output.'/'.$filename);
			
			/* UNPACK RAR & COPY FROM TEMP */
			$rar_file = rar_open($output.'/'.$filename);
			$list = rar_list($rar_file);
			
			foreach($list as $file) {
				$entry = rar_entry_get($rar_file, $file->getName());
				$entry -> extract($output);
			}
			rar_close($rar_file);

			$rrmdir_arr = array('content','example_content');
			foreach($rrmdir_arr as $rrmdirs){
				rrmdir($rrmdirs);
				system('mv '.$output.'/'.$filename2.'/'.$rrmdirs.' .');
			}
			
		} else {
			$error_message = 'Error Uploading File..';
			echo "<script> window.location.href='".$_SERVER['PHP_SELF']."?page=".$_GET['page']."&error=1&error_message=".$error_message."'; </script>";
		}
	}
Edited by lewisstevens1
Link to comment
https://forums.phpfreaks.com/topic/290456-php-help/#findComment-1487840
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.