function getTempName($fileName = '', $dir = '') { if (empty($dir)) { $dir = getTempDir(); } $fileName = basename($fileName); if (empty($fileName)) { $fileName = time(); } $fileName = preg_replace('|\\..*$|', '.tmp', $fileName); $fileName = $dir . getUniqueFileName($dir, $fileName); touch($fileName); return $fileName; }
/** * Inserts the information about an attachment into the db * * @param int $fkid the foreign key id (attachments.fk_id) * @param string $fktableName the tablename to which the $id refers to (attachments.fk_table) * @param string $title the title used for the attachment * @param array $fInfo should be $_FILES in most cases * * @return int returns true if the information was successfully stored, false else * **/ public function insertAttachment($fkid, $fkTableName, $title, $fInfo) { $fName = isset($fInfo['name']) ? $fInfo['name'] : null; $fType = isset($fInfo['type']) ? $fInfo['type'] : ''; $fSize = isset($fInfo['size']) ? $fInfo['size'] : 0; $fTmpName = isset($fInfo['tmp_name']) ? $fInfo['tmp_name'] : ''; $fContents = null; $fExt = getFileExtension(isset($fInfo['name']) ? $fInfo['name'] : '', "bin"); $destFPath = null; $destFName = getUniqueFileName($fExt); if ($this->repositoryType == TL_REPOSITORY_TYPE_FS) { $destFPath = $this->buildRepositoryFilePath($destFName, $fkTableName, $fkid); $fileUploaded = $this->storeFileInFSRepository($fTmpName, $destFPath); } else { $fContents = $this->getFileContentsForDBRepository($fTmpName, $destFName); $fileUploaded = sizeof($fContents); if ($fileUploaded) { @unlink($fTmpName); } } if ($fileUploaded) { $attachment = new tlAttachment(); $fileUploaded = $attachment->create($fkid, $fkTableName, $fName, $destFPath, $fContents, $fType, $fSize, $title) >= tl::OK; if ($fileUploaded) { $fileUploaded = $attachment->writeToDb($this->db); } else { @unlink($destFPath); } } return $fileUploaded; }