I am able to upload images to an S3 bucket successfully, but I am really stumped as to how it's working.

I have a file input in my template, and in my controller I am doing this:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = craft()->config->get('section', 'mySection');
$criteria->limit = 1;
$criteria->status = null;
$criteria->authorId = craft()->request->getPost('userId');
// have also tried getting the user like this for sanity:
// get the same id either way
//$user = craft()->userSession->getUser();
//$criteria->authorId = $user->id; 

$entry = $criteria->first();

if ($entry) {
    $folder = craft()->assets->findFolder(array(
        'sourceId' => $this->assetSourceId,
    error_log($folder->id) // 207

    $upload = craft()->assets->insertFileByLocalPath(
     if ($upload->isSuccess()) {

Everything works great. The image is uploaded to the correct S3 bucket. But when doing the same in the CP the folder ID is actually different. It looks like it should be 27?

enter image description here

I'm also trying to upload another file my-file.json from the same front-end form. I am using the same technique above to get the folder ID, and it is returning the same number (207). However the asset is getting uploaded to the correct folder (207). But in my case that is actually the incorrect folder. It should be uploading to 27.

Is there a more effective way to get the correct folder Id? Thank you for any suggestions!

FWIW, folder 207 is the first folder in my asset list...


I'm just going to keep updating this as I fumble through it and revise it to an answer when I figure it out.

Using craft()->assets->findFolder() will find the first folder that matches a given criteria.

Since in my above example, I am only passing in the source Id, that means that I'm only going to get back the first folder for that resource. (In my case that would be folder 207).

By changing the findFolder() params to this:

$user = craft()->userSession->getUser();
$folder = craft()->assets->findFolder(array(
    'sourceId' => $this->assetSourceId,
    'name' => $user->username

I am now getting the correct folder Id back. How I was able to upload images successfully prior to this is still a mystery.

1 Answer 1

if ($upload->isSuccess()) {
     $fileId = $upload->getDataItem('fileId');
     $asset = craft()->assets->getFileById($fileId);
     $folderId = $asset->getFolder()->getAttribute('id');

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.