Gdn_Upload::copyLocal PHP Method

copyLocal() public method

Copy an upload locally so that it can be operated on.
public copyLocal ( string $Name ) : string
$Name string Filename to be copied.
return string Local file path.
    public function copyLocal($Name)
    {
        $Parsed = self::parse($Name);
        $LocalPath = '';
        $this->EventArguments['Parsed'] = $Parsed;
        $this->EventArguments['Path'] =& $LocalPath;
        $this->fireAs('Gdn_Upload')->fireEvent('CopyLocal');
        if (!$LocalPath) {
            $LocalPath = PATH_UPLOADS . '/' . $Parsed['Name'];
        }
        return $LocalPath;
    }

Usage Example

 /**
  * Do code checks on an uploaded addon.
  *
  * @param int $AddonID Addon to check.
  * @param bool|false $SaveVersionID Whether to save the version id.
  * @throws Exception Addon not found.
  */
 public function check($AddonID, $SaveVersionID = false)
 {
     $this->permission('Addons.Addon.Manage');
     if ($SaveVersionID !== false) {
         // Get the version data.
         $Version = $this->AddonModel->SQL->getWhere('AddonVersion', array('AddonVersionID' => $SaveVersionID))->firstRow(DATASET_TYPE_ARRAY);
         $this->AddonModel->save($Version);
         $this->Form->setValidationResults($this->AddonModel->validationResults());
     }
     $Addon = $this->AddonModel->getID($AddonID, false, ['GetVersions' => true]);
     $AddonTypes = Gdn::sql()->get('AddonType')->resultArray();
     $AddonTypes = Gdn_DataSet::index($AddonTypes, 'AddonTypeID');
     if (!$Addon) {
         throw notFoundException('Addon');
     }
     // Get the data for the most recent version of the addon.
     $upload = new Gdn_Upload();
     // Also used per version below.
     $Path = $upload->copyLocal($Addon['File']);
     $AddonData = arrayTranslate((array) $Addon, array('AddonID', 'AddonKey', 'Name', 'Type', 'Description', 'Requirements', 'Checked'));
     try {
         $FileAddonData = UpdateModel::analyzeAddon($Path);
         if ($FileAddonData) {
             $AddonData = array_merge($AddonData, arrayTranslate($FileAddonData, array('AddonKey' => 'File_AddonKey', 'Name' => 'File_Name', 'File_Type', 'Description' => 'File_Description', 'Requirements' => 'File_Requirements', 'Checked' => 'File_Checked')));
             $AddonData['File_Type'] = valr($FileAddonData['AddonTypeID'] . '.Label', $AddonTypes, 'Unknown');
         }
         $upload->delete($Path);
     } catch (Exception $Ex) {
         $AddonData['File_Error'] = $Ex->getMessage();
     }
     $this->setData('Addon', $AddonData);
     // Go through the versions and make sure we get the versions to check out.
     $Versions = array();
     foreach ($Addon['Versions'] as $Version) {
         $Version = $Version;
         $Path = $upload->copyLocal($Version['File']);
         try {
             $VersionData = arrayTranslate((array) $Version, array('AddonVersionID', 'Version', 'AddonKey', 'Name', 'MD5', 'FileSize', 'Checked'));
             $FileVersionData = UpdateModel::analyzeAddon($Path);
             $FileVersionData = arrayTranslate($FileVersionData, array('Version' => 'File_Version', 'AddonKey' => 'File_AddonKey', 'Name' => 'File_Name', 'MD5' => 'File_MD5', 'FileSize' => 'File_FileSize', 'Checked' => 'File_Checked'));
             $upload->delete($Path);
         } catch (Exception $Ex) {
             $FileVersionData = array('File_Error' => $Ex->getMessage());
         }
         $Versions[] = array_merge($VersionData, $FileVersionData);
     }
     $this->setData('Versions', $Versions);
     $this->addModule('AddonHelpModule');
     $this->render();
 }
All Usage Examples Of Gdn_Upload::copyLocal