Pimcore\Model\Asset::create PHP Method

create() public static method

Helper to quickly create a new asset
public static create ( integer $parentId, array $data = [], $save = true ) : Asset
$parentId integer
$data array
return Asset
    public static function create($parentId, $data = [], $save = true)
    {
        // create already the real class for the asset type, this is especially for images, because a system-thumbnail
        // (tree) is generated immediately after creating an image
        $class = "Asset";
        if (array_key_exists("filename", $data) && (array_key_exists("data", $data) || array_key_exists("sourcePath", $data) || array_key_exists("stream", $data))) {
            if (array_key_exists("data", $data) || array_key_exists("stream", $data)) {
                $tmpFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/asset-create-tmp-file-" . uniqid() . "." . File::getFileExtension($data["filename"]);
                if (array_key_exists("data", $data)) {
                    File::put($tmpFile, $data["data"]);
                } else {
                    $streamMeta = stream_get_meta_data($data["stream"]);
                    if (file_exists($streamMeta["uri"])) {
                        // stream is a local file, so we don't have to write a tmp file
                        $tmpFile = $streamMeta["uri"];
                    } else {
                        // write a tmp file because the stream isn't a pointer to the local filesystem
                        rewind($data["stream"]);
                        $dest = fopen($tmpFile, "w+", false, File::getContext());
                        stream_copy_to_stream($data["stream"], $dest);
                        fclose($dest);
                    }
                }
                $mimeType = Mime::detect($tmpFile);
                unlink($tmpFile);
            } else {
                $mimeType = Mime::detect($data["sourcePath"], $data["filename"]);
                if (is_file($data["sourcePath"])) {
                    $data["stream"] = fopen($data["sourcePath"], "r+", false, File::getContext());
                }
                unset($data["sourcePath"]);
            }
            $type = self::getTypeFromMimeMapping($mimeType, $data["filename"]);
            $class = "\\Pimcore\\Model\\Asset\\" . ucfirst($type);
            if (array_key_exists("type", $data)) {
                unset($data["type"]);
            }
        }
        $asset = new $class();
        $asset->setParentId($parentId);
        $asset->setValues($data);
        if ($save) {
            $asset->save();
        }
        return $asset;
    }

Usage Example

 public function importUrlAction()
 {
     $success = true;
     $data = Tool::getHttpData($this->getParam("url"));
     $filename = basename($this->getParam("url"));
     $parentId = $this->getParam("id");
     $parentAsset = Asset::getById(intval($parentId));
     $filename = File::getValidFilename($filename);
     $filename = $this->getSafeFilename($parentAsset->getFullPath(), $filename);
     if (empty($filename)) {
         throw new \Exception("The filename of the asset is empty");
     }
     // check for duplicate filename
     $filename = $this->getSafeFilename($parentAsset->getFullPath(), $filename);
     if ($parentAsset->isAllowed("create")) {
         $asset = Asset::create($parentId, array("filename" => $filename, "data" => $data, "userOwner" => $this->user->getId(), "userModification" => $this->user->getId()));
         $success = true;
     } else {
         \Logger::debug("prevented creating asset because of missing permissions");
     }
     $this->_helper->json(array("success" => $success));
 }
All Usage Examples Of Pimcore\Model\Asset::create