4

I've found information on how to upload an asset as part of creating an entry. What I am trying to do is upload an asset from the frontend without creating an entry. I have a logged in user, and what I would like them to be able to do is upload an asset to a folder that is unique to that user (folder name = username). Help?

9

That's not going to be possible to do without a plugin.

The plugin would need to provide its own Controller which does the following:

  1. Grab info about the uploaded file using UploadedFile::getInstanceByName()
  2. Determine the ID of the asset folder you want to upload the file to using AssetsService::findFolder()
  3. Move the file into the folder using AssetsService::insertFileByLocalPath()

Here’s an example:

// Get info about the uploaded file
$file = UploadedFile::getInstanceByName('fileInputName');
$tempFilePath = $file->getTempName();

// Find the target folder
$folder = craft()->assets->findFolder(array(
    'sourceId' => 1,
    'path'     => 'subfolder/path/'
));

// Turn the file into a managed asset within that folder
craft()->assets->insertFileByLocalPath(
    $tempFilePath,
    $file->getName(),
    $folder->id,
    AssetConflictResolution::KeepBoth);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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