Upload folder structure

Upload folder structure was changed in version 12 of Sugar. The customizations has to be modified in case of direct access to the file with guid from the folder.

New structure: upload/<3char> directories e.g upload/394/e2a18394-da7f-11e9-8a56-06c3e8e0c8ed

In case your upload folder is still has old structure after the upgrade due to some reason. Please execute this Sugar written script to resolve the issue.

<?php

define('sugarEntry', true);
define('ENTRY_POINT_TYPE', 'api');

require_once 'include/entryPoint.php';

$logger = LoggerManager::getLogger();

$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) != 'cli') {
    $logger->fatal('Invalid Call to CLI script');
    exit(1);
}
$GLOBALS['current_user'] = BeanFactory::getBean('Users', 1);
global $db, $timedate;

writeLog("-----------------------------------------------");
writeLog("Start " . date('Y-m-d h:i:s'));
writeLog("-----------------------------------------------");



function run()
{
    $uploadDir = UploadStream::getDir();
    $dir = opendir($uploadDir);
    if (!$dir) {
        writeLog("Cannot open upload directory");
        return;
    }
    $total = 0;
    while (($file = readdir($dir)) !== false) {
        $pathFrom = $uploadDir . DIRECTORY_SEPARATOR . $file;
        if (!is_guid($file) || !is_file($pathFrom)) {
            continue;
        }
        $subdirName = makeDirName($file);
        $nameTo = $subdirName . DIRECTORY_SEPARATOR . $file;
        sugar_mkdir($uploadDir . DIRECTORY_SEPARATOR . $subdirName);
        sugar_rename($pathFrom, $uploadDir . DIRECTORY_SEPARATOR . $nameTo);
        $total++;
        if ($total % 1000 === 0) {
            writeLog("Moved {$file} to {$nameTo}, processed {$total} files");
        }
    }
    writeLog("Done, processed {$total} files");
    closedir($dir);
}

function makeDirName($filename)
{
    return substr($filename, 5, 3);
}



run();


writeLog("-----------------------------------------------");
writeLog("End " . date('Y-m-d h:i:s'));
writeLog("-----------------------------------------------");

/**
 * Writing the message
 */
function writeLog($message)
{
    $logger = LoggerManager::getLogger();
    echo $message;
    echo PHP_EOL; 
    $logger->fatal($message);
}

Create php file and Go to sugar root directory and run php file from cli

Parents Reply Children
No Data