1

I'm currently making a module that I'm hoping is going to create entries and assets from an API. I've got the entries part working and now I'm trying to create an asset from a URI of an image.

I've tried to use this method How do I upload an asset in Craft 3, via PHP but I can't get it to work I'm just wondering what I'm doing wrong? Do I need to be doing something with the temp file maybe? I've put this within the same function that I'm using to create the entries.

Eventually I need to loop through an array of objects where the images are stored and create an asset from each of the images but for the moment I'm just trying to get it to create one asset from a single URI of an image

    $uploadedFile = "https:\/\/images.fakeuritoprotectsecurity.com\/images\/1\/20\/33\/0123456_789_1_XLARGE.jpg";
    $folderId = 1;

    if (empty($folderId)) {
        throw new BadRequestHttpException('No target destination provided for uploading');
    }

    if ($uploadedFile === null) {
        throw new BadRequestHttpException('No file was uploaded');
    }

    $assets = Craft::$app->getAssets();

   if ($uploadedFile->getHasError()) {
       throw new UploadFailedException($uploadedFile->error);
   }

    // Move the uploaded file to the temp folder
    if (($tempPath = $uploadedFile->saveAsTempFile()) === false) {
        throw new UploadFailedException(UPLOAD_ERR_CANT_WRITE);
    }

   // Move the uploaded file to the temp folder
   if (empty($folderId)) {
       throw new BadRequestHttpException('The target destination provided for uploading is not valid');
   }

    $folder = $assets->findFolder(['id' => $folderId]);

    if (!$folder) {
        throw new BadRequestHttpException('The target folder provided for uploading is not valid');
    }

    // Check the permissions to upload in the resolved folder.
    $filename = Assets::prepareAssetName($uploadedFile->name);

    $asset = new Asset();
    $asset->tempFilePath = $tempPath;
    $asset->filename = $filename;
    $asset->newFolderId = $folder->id;
    $asset->volumeId = $folder->volumeId;
    $asset->avoidFilenameConflicts = true;
    $asset->setScenario(Asset::SCENARIO_CREATE);

    $result = Craft::$app->getElements()->saveElement($asset);
    if (!$result) {
        Craft::error('Couldn’t save the entry "'.$asset->filename.'"', __METHOD__);
    }

Any help is really appreciated, thanks

1 Answer 1

1

I think your error is something like "newlocation cannot be empty". I solved this in the following way:

$asset = new Asset();
$asset->tempFilePath = $tempPath;
$asset->filename = $fileName;
$asset->folderId = null;
$asset->newFolderId = $folder->id;
$asset->kind = "Image";
$asset->title = $assetTitle;
$asset->avoidFilenameConflicts = true;
$asset->setVolumeId($folder->volumeId);
$asset->setScenario(Asset::SCENARIO_CREATE);

$asset->validate();
Craft::$app->getElements()->saveElement($asset, false);

Hope this will get the work done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.