Introduction:
This app download, extract the gunzip, backup the given config folder (single folder only) and replace the existing app file with updated files.
The app is tested on updating wordpress files. You can use if for update of owncloud, piwik, or modify it to upload files to your host server from your client computer. It make use of cURL and phar class of PHP. It works for gunzip update files only. For zip files, you would need to modify code of extr.php.
Main Code:
Important code for this app is:
- downloading file to host server
- extracting the gunzip
- and moving the backup and update files
PHP code for uploading a gunzip file to your host server with progress bar javascript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php set_time_limit(0); ob_flush(); flush(); $fp = fopen (dirname(__FILE__) . $gzname, 'w+'); $ch = curl_init($remoteFile); curl_setopt($ch, CURLOPT_TIMEOUT, $ctimeout); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress'); curl_setopt($ch, CURLOPT_NOPROGRESS, false); // needed to make progress function work curl_exec($ch); curl_close($ch); fclose($fp); function progress($download_size, $downloaded, $upload_size, $uploaded) { if($download_size > 0) $ds = $downloaded / $download_size * 100; $ds = round($ds*100)/100 ."%"; // Javascript for updating the progress bar and information echo '<script language="javascript"> document.getElementById("dprogress").innerHTML="<div style=\"width:'.$ds.';background-color:#ddd;\"> </div>"; //document.getElementById("dinformation").innerHTML="'.$downloaded.' row(s) processed."; document.getElementById("dinformation").innerHTML="'.$ds.' downloaded to server."; </script>'; ob_flush(); flush(); //sleep(1); // just to see effect } echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>'; echo "Download Done. <br />\n"; ob_flush(); flush(); ?> |
PHP code for extracting gunzip file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php try { echo "starting extraction .... <br />\n"; // decompress from gz $p = new PharData($extsrc); $p->decompress(); // creates /path/to/my.tar // unarchive from the tar $phar = new PharData($extsrctar); $phar->extractTo($extdst); } catch (Exception $e) { // handle errors echo "error ".$e."<br />\n"; } unlink($extsrctar); unlink($extsrc); echo "extraction command completed. <br />\n"; ?> |
PHP code for moving and backup of folder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php echo "backing up the given (config) folder ...<br />\n"; rename($dstwebpath . $dstfoldername . $cfgfolder,$webpath . '/tmp' . $cfgfolderbak); echo "moving to destination directory ... <br />\n"; rrmdir($dstfolder); rename($srcfolder,$dstfolder); echo "restoring the given (config) folder ...<br />\n"; rrmdir($dstwebpath . $dstfoldername . $cfgfolder); rename($webpath . '/tmp'. $cfgfolderbak,$dstwebpath .$dstfoldername . $cfgfolder); echo "end of program."; // Function to remove folders and files function rrmdir($dir) { if (is_dir($dir)) { $files = scandir($dir); foreach ($files as $file) if ($file != "." && $file != "..") rrmdir("$dir/$file"); rmdir($dir); } else if (file_exists($dir)) unlink($dir); } ?> |
Installation:
- Download the app from http://enggprog.com/Download/Projects/upload/uploadapp.zip and extract on your computer.
- Edit config.php and change following variables:
- $remoteFile = url of download package.
- $webpath = full absolute webpath of the app including the folder.
- $dstwebpath = full absolute webpath of the owncloud/req app including the folder. Files will be extracted here.
- $srcfoldername = extracted files will be in this folder. It shall be the name of root folder of update package.
- $dstfoldername = extracted files will be moved to this folder. This folder must exist on your server.
- $cfgfolder = single folder (usually config folder) that is inside $dstfoldername folder to backup during update.
- $cfgfolderbak = name of backup folder
- $ctimeout = timeout of download time, increase for large files.
- Copy the url of index.php file of this app and paste it in your browser.
Download:
Download source code of uploadapp v0.1
Note:
- Incorrect folder/file name can produce error.
- Backup your full app directory before first time use.