0

I am attempting to add a file to a node. I am using the following code which is saving the file to the file_managed table in Drupal without issue:

//query old DB for the pieces of the file I need happens above this

foreach($result as $r) {
  echo "Processing nid: " . $r->nid . "\r\n";

  //update nodes with the thumbnails
  if ($node = node_load($r->nid)) { //make sure there is a node to load
    $file_temp = file_get_contents('http://www.example.org/' . str_replace(' ', '%20', $r->filepath));
    $file_temp = file_save_data($file_temp, 'public://' . str_replace(' ', '_', $r->filename), FILE_EXISTS_REPLACE);
    $node->field_thumbail['und'][0] = array (
      'uid' => 1,
      'fid' => $file_temp->fid,
      'uri' => $file_temp->filename,
      'filemime' => $file_temp->filemime,
      'status' => 1,
      'display' => 1,
    );
    print_r($file_temp);
    node_save($node);
  }
}

That saves the file just fine, and appears to save node with the correct information too. However, Drupal renders it like this:

View of the file field in edit view

The file exists at the URL, and entering the file URL in the browser renders the file. There is an entry in the {file_managed} table and the data is saved to the node. That is, if I do a:

drush php-eval 'print_r(node_load($nid))'

replacing $nid with my node id, the field_thumbnail looks to be properly populated.

There is not, however, an entry in the files table for this fid.

I'm wondering what the difference between files_managed and files tables are? Do I need to also save it to the files table for Drupal to render it properly?

2
  • 1
    Drupal 7 doesn't have a files table...maybe it's a remnant from a D6 install?
    – Clive
    Mar 9, 2015 at 19:48
  • That would make sense, this is an upgrade from a D6 site. Any thoughts on why the files wouldn't be rendering? They are in the file_managed table, and exist on the server at the correct path. Thanks
    – Ryan Price
    Mar 10, 2015 at 13:02

0

Your Answer

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

Browse other questions tagged or ask your own question.