Drupal core 7.66 / Drupal core 8.6.15 Updater

This code for rapidly update Drupal core 7 and Drupal core 8. The progress only takes you a few seconds to a few minutes.

  1. Place updatecore.php into the drupal website folder.
  2. Browse http://xxxx.com/updatecore.php and wait for a few seconds to a minute.
  3. After finishing the update, you may need to go to the http://xxxx.com/update.php and update the database.
  4. If your have installed dependencies using composer, you have to go to the terminal and run "composer update --with-dependencies"

Download or Download from Github
<?php
// 2019-04-22
// >= PHP 5.1.0
// Drupal core 8.6.15
// Drupal core 7.66

//Setting
$latest_drupal8_url="https://ftp.drupal.org/files/projects/drupal-8.6.15.zip";
$folders_to_copy_v8=[
  'sites/default/files',
  'core/themes',
  'modules'
];
$files_to_copy_v8=[
  'sites/default/settings.php',
  '.htaccess',
  'robots.txt',
  'web.config',
  //'composer.lock',
  'composer.json'
];
$latest_drupal7_url="https://ftp.drupal.org/files/projects/drupal-7.66.zip";
$folders_to_copy_v7=[
  'sites/default/files',
  'sites/all',
];
$files_to_copy_v7=[
  'sites/default/settings.php',
  '.htaccess',
  'robots.txt',
  'web.config',
  //'composer.lock',
  'composer.json'
];

///////Auto detect version
$drupal_version=v_auto_detect();
$webrootname=basename(__DIR__);
$backup_filename='../'.$webrootname.'_'.date('Ymd_His');
if($drupal_version!=8 && $drupal_version!=7){
  echo 'Drupal version is not supported'.'<br/>';
  exit();
}

///////download drupal core
if($drupal_version==8)
  $latest_drupal_url=$latest_drupal8_url;
else if($drupal_version==7)
  $latest_drupal_url=$latest_drupal7_url;
$target_path='../latest_drupal_core.zip';
if(!downloadFile($latest_drupal_url,$target_path))
{
  echo 'Error while downloading Drupal core file'.'<br/>';
  exit();
}
if(!file_exists($target_path))
{
  echo 'Error while downloading Drupal core file. Please check whether the parent folder is writeable.'.'<br/>';
  exit();
}

///////unzip
$unzip_path='../latest_drupal_core';
if(!unzip($unzip_path,$target_path))
  exit();
@unlink($target_path);
$unzipped_path=$unzip_path.'/'.preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($latest_drupal_url));

///////copy necessary files and folders
if($drupal_version==8)
{
  $files_to_copy=$files_to_copy_v8;
  $folders_to_copy=$folders_to_copy_v8;
}
elseif($drupal_version==7)
{
  $files_to_copy=$files_to_copy_v7;
  $folders_to_copy=$folders_to_copy_v7;
}

//////find another files
$sourceDirList=[];
if ($handle = opendir($unzipped_path)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            $sourceDirList[]=$entry;
        }
    }
    closedir($handle);
}
if ($handle = opendir('.')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            if(!in_array($entry,$sourceDirList) && $entry!=basename(__FILE__) && pathinfo($entry, PATHINFO_EXTENSION)!='swp')
            {
              if(is_dir($entry))
              {
                $folders_to_copy[]=$entry;
              }
              else{
                $files_to_copy[]=$entry;
              }
            }
        }
    }
    closedir($handle);
}


foreach ($files_to_copy as $file) {
  if(file_exists($file) && !copy($file,$unzipped_path.'/'.$file))
  {
    echo 'There was a problem while copying '.$file.' to '.$unzipped_path.'/'.$file.'<br/>';
    exit();
  }
}
foreach ($folders_to_copy as $folder) {
  full_copy($folder,$unzipped_path.'/'.$folder);
}

///////// make a backup file
if(!rename('../'.$webrootname,$backup_filename))
{
  echo 'Error: Cannot create backup file'.'<br/>';
  exit();
}

///////// update completed, move new version to webroot
if(!rename($unzipped_path,'../'.$webrootname))
{
  //rollback
  if(!rename($backup_filename,'../'.$webrootname))
  {
    echo 'Error: Cannot move files to webroot'.'<br/>';
  }
  else
    echo 'Critical error!!! Cannot move files to webroot, please recover the webroot folder manually.'.'<br/>';
  exit();
}

//////////End
rmdir($unzip_path);
echo 'Update core finished'.'<br/>';
echo 'Go to <a href="/update.php">update.php</a><br/>';
echo 'You may also need to run "composer update --with-dependencies" in the terminal.<br/>';
exit();

////////////////////////////////////////////////////////////////////////////////////////////////
function v_auto_detect(){
  if(file_exists('sites/all/themes'))
  {
    echo 'Detected Version: 7<br/>';
    return 7;
  }
  else if(file_exists('core'))
  {
    echo 'Detected Version: 8<br/>';
    return 8;
  }

  return false;
}

function unzip($outpath,$zippath)
{
  $unzip = new ZipArchive;
  $out = $unzip->open($zippath);
  if ($out === TRUE) {
    $unzip->extractTo($outpath);
    $unzip->close();
    return true;
  } else {
    echo 'Error while unzipping the file.'.PHP_EOL;
    return false;
  }
}
function downloadFile($url, $path)
{
    $newfname = $path;
    $file = fopen ($url, 'rb');
    if ($file) {
        @unlink($newfname);
        $newf = fopen ($newfname, 'wb');
        if ($newf) {
            while(!feof($file)) {
                if(!fwrite($newf, fread($file, 1024 * 8), 1024 * 8))
                {
                  echo 'Cannot write to the path '.$path.'<br/>';
                }
            }
        }
        else {
          echo 'Cannot create the path '.$path.'<br/>';
        }
    }
    else{
      return false;
    }

    if ($file) {
        fclose($file);
    }
    if ($newf) {
        fclose($newf);
    }
    return true;
}
function full_copy( $source, $target ) {
    if ( is_dir( $source ) ) {
        @mkdir( $target );
        $d = dir( $source );
        while ( FALSE !== ( $entry = $d->read() ) ) {
            if ( $entry == '.' || $entry == '..' ) {
                continue;
            }
            $Entry = $source . '/' . $entry;
            if ( is_dir( $Entry ) ) {
                full_copy( $Entry, $target . '/' . $entry );
                continue;
            }
            copy( $Entry, $target . '/' . $entry );
        }

        $d->close();
    }else {
        copy( $source, $target );
    }
}
?>


suggest@drupalupdate.uwebweb.com

Donate