I have a plugin that creates a local JSON file and uploads it to S3 using insertFileByLocalPath. The file is created, saved, and uploaded to s3 successfully. However, S3 detects the content-type to be text/plain and not application/json. Does anyone know of a way to force the content-type when creating an asset?

    $jsonStr = json_encode($arrayToBecomeJson);

    $fileName = $myFileName . '.json';

    $tempPath = AssetsHelper::getTempFilePath('json');

    file_put_contents($tempPath, $jsonStr);

    $assetSaveSuccess = craft()->assets->insertFileByLocalPath(

1 Answer 1


While Craft 3 will be smarter about this, this is not currently possible in Craft 2.

What you can do, though, is modify craft/app/helpers/IOHelper.php::getMimeType() function so that it always returns the mime type by extension instead by the content, since the core problem is that FileInfo extension cannot detect JSON by looking at the file contents. I'm a bit doubtful of any future releases for Craft 2 that affect this file, but, for posterity, probably better to write down any changes you make somewhere in case they get reverted.

  • Thanks Andris! For now we will likely run a script outside of Craft that adjusts the content-type so we don't interfere with any updates. Looking forward to the Craft 3 improvements! Jan 2, 2017 at 18:25

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.