Is it possible to grab file (mp3) that has been submitted as part of an entry and post it to a secondary location (Soundcloud via their API) before it gets stored to my asset source at Amazon S3?

To complicate things a bit, I am uploading multiple files per entry, each one having its own field. One is the produced file (field name mp3File) selection) and the others are the raw recordings (field name rawFiles).

It is produced file (mp3File) that I am wanting to push to SoundCloud with their API.

I have a handle on how the SoundCloud API works, what I need is to pass the file path to the SoundCloud API. I think it is probably better to do this while the file still resides on the server before it makes its way to Amazon. Or it could be that I don't understand the flow here at all.


So this works. Now that I have the understanding of how things fire I can clean this up. I am open to better solutions.

    craft()->on('entries.onSaveEntry', function(Event $event)

        $entry = $event->params['entry'];    
        if($event->params['isNewEntry'] && $entry->getSection() == 'Episode')



        $clientID       =   'xxxxxxxxxxxxxxx';
        $clientSecret   =   'xxxxxxx';
        $username       =   'xxxx';
        $password       =   'xxxx';

        $facade = new \Njasm\Soundcloud\SoundcloudFacade($clientID, $clientSecret);
        $facade->userCredentials($username, $password); 

        $mp3File = $entry->mp3File->first()->filename;
        $url = $entry->mp3File->first()->getUrl();

        $mp3TempFile = "/var/www/pathway/craft/storage/tmp/".$mp3File;

        // download the file from amazon s3 and push it to soundcloud
        $fp = file_put_contents($mp3TempFile, fopen($url, 'r'));

        $params = array(
            'track[title]' => iconv('UTF-8', 'ASCII//TRANSLIT', $entry->title),
            'track[sharing]' => 'public',
            //'track[asset_data]' => $mp3Filepath,
            'track[description]' => iconv('UTF-8', 'ASCII//TRANSLIT', strip_tags($entry->summary)),
            'track[track_type]' => 'podcast',
            'track[genre]' => 'Learning',
            'track[license]' => 'all-rights-reserved'

        $response = $facade->upload($mp3TempFile, $params);
        $fp = unlink($mp3TempFile);


  • BTW, if anyone knows a craft path that gets me here /var/www/pathway/craft/storage, (something similar to craft()->path->getPluginsPath()) that would be helpful. Apr 19 '16 at 15:48
  • 1
    The CRAFT_STORAGE_PATH constant should do the trick? Apr 19 '16 at 18:07

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.