I'm looking to do something like this

$fid = 15;
$file = (array)file_load($fid);
$content = drupal_render($file);
echo $content;

And specifically this fid is from the 7.x-2.x version of the media module.

3 Answers 3


This works for me. For videos via the media module:

  $file = file_load($fid);
  if (strpos($file->filemime, 'video/') !== 0) {
  $key = 'media_' . substr($file->filemime, 6) . '_video';
  $formatter_info = file_info_formatter_types($key);
  $content = array();
  $content['#theme'] = $key;
  $content['#uri'] = $file->uri;
  if (isset($formatter_info['default settings'])) {
    $content['#options'] = $formatter_info['default settings'];

  $rendered = drupal_render($content);
  return $rendered;

And for images; this shows you what presets are available (#style_name)

$styles = image_styles();
echo '<pre>' . print_r($styles, TRUE) . '</pre>';

And this will render the file

$file = file_load($fid);
$image = image_load($file->uri);
$content = array(
  'file' => array(
    '#theme' => 'image_style',
    '#style_name' => 'large',
    '#path' => $image->source,
    '#width' => $image->info['width'],
    '#height' => $image->info['height'],
echo drupal_render($content);

Note that image_load performs I/O.

And for the reverse; given a filename get a fid.

$query = new EntityFieldQuery();
$result = $query
  ->entityCondition('entity_type', 'file')
  ->propertyCondition('filename', basename($filename))
foreach ($result['file'] as $values) {
  $fid = $values->fid;
echo $fid

Given media embed code, get a FID.

$file = media_parse_to_file($embed_code);
if (empty($file->fid)) {
  return FALSE;
return $file->fid;
  • Can you please explain "$key = 'media_' . substr($file->filemime, 6) . '_video';" ? I am trying to render a video from fid. I don't want to use any video field and I don't have any custom video formats. Aug 3, 2016 at 9:24
  • 1
    @TejasVaidya if $file->filemime = video/youtube then the key will end up being 'media_youtube_video' which will use the theme function inside of drupal.org/project/media_youtube (see media_youtube_theme())
    – mikeytown2
    Aug 3, 2016 at 18:20
  • 1
    If you want to render the original image just use #theme => 'image' and remove '#style_name' => 'large' Mar 17, 2022 at 15:28

You are misapplying drupal_render() in your example. drupal_render() holds the content to be displayed and the instructions for how to render it in an array so that all content can be modified by other modules, until the last moment before displaying. Everything is loaded in the drupal_render() argument &$elements, which is the argument for the function. drupal_render() does not return rendered elements from variables passed in arguments.

Look at the theme API for a series of functions that will provide HTML for various content elements including files.


The function you are probably looking for is file_get_content_headers(), which sets the headers for allowing a file to be downloaded, or (if the file is a text file, or an image) to be viewed inline.

The function requires a file object as returned by file_load_multiple(), file_load(), or entity_load('file').

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.