I am working on a module that is transferring files from my old site (hosted on the same server) to a new drupal based site. The files will be associated with a single content type (the content type has multipel file fields associated with it). I've written code to read a directory and then loop through the files, associating the files with the appropriate file file. So far the files are getting transferred correctly, but they aren't being associated with the new node. What am I missing
$result = new stdClass;
$result->type = "results";
node_object_prepare($result);
//add values to text fields here
...
//open the source directory
$d = dir($seasonDir);
while(false !== ($entry = $d->read())) {
if($entry != "." && $entry != "..") {
//set the file location
$fileLocation = $seasonDir . "/" . $entry;
//get the file contents
$fileContents = file_get_contents($fileLocation);
//save the file
$file = file_save_data($fileContents,'public://results/'.$entry,FILE_EXISTS_RENAME);
//store the file for copying to a subdirectory later
$result_files[] = $file;
//create an array to assign to the various file fields
$file_details = array(LANGUAGE_NONE => array('0' => (array)$file));
switch($entry) {
case "results.inc.html":
$result->field_results_html = $file_details;
break;
case"clubs.inc.html":
$result->field_clubs_html = $file_details;
break;
case "events.inc.html":
$result->field_events_html = $file_details;
break;
case "index.xml":
$result->field_results_index = $file_details;
break;
case "events.xml":
$result->field_results_events = $file_details;
break;
case "clubs.xml":
$result->field_results_clubs = $file_details;
break;
case "results.xml":
$result->field_results = $file_details;
break;
default:
//because the resuls_files field allows multiple files, this
//field has to use a different method of assigning the file
$result->results_files[LANGUAGE_NONE][] = (array)$file;
break;
}
}
}
$d->close();
node_save($result);
//now move the files to the folder using the node ID as the subfolder
$newdir = 'public://results/' . $result->nid;
if(file_prepare_directory($newdir, FILE_CREATE_DIRECTORY)) {
//move the files into this directory
foreach($result_files as $file) {
file_move($file, $newdir . '/' . $file->filename, FILE_EXISTS_REPLACE);
}
}
else {
drupal_set_message("could not create $newdir");
}
At the end, all the nodes have been created, the files have been moved to the correct sub directory, but the node doesn't show any attached files. What am I missing?
file_save_data
thenfile_move
?