I have a use case for this.

I'm creating files on the fly in an RTMP server (Red5 in this case) and would like to move these files from the temporary directory into the files directory for Drupal and manage them in the Drupal database, instead of having to manage them myself. Currently I have to write some code that is currently in the file_save_upload function over again, which is basically to take a static file reference and create a file object from it, then save it to the database.

It would be nice for contributed modules if this code logic was separated from file_save_upload. It's obviously not an enormous feature, but it seems silly for each module to have to copy and paste code from file.inc just because it's inaccessible in a specialized function.

Something like:

<?php

/**
* Returns a file object
* @param $path A path that leads to a file on the server
* @param $status The status of the file either 0 or 1
* @return $file A fully formed file object
*/
function file_create_object($path, $status = FILE_STATUS_PERMANENT, $save = FALSE) {
// Begin building file object.
$file = new stdClass();
$file->uid = $user->uid;
$file->status = 0;
$file->filename = basename($path);
$file->filepath = $path;
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = filesize($file->filename);

if ($save) {
file_save($file);
}

return $file;
}

This is obviously a very simplified version of what the final function would be like, but you get the idea.

Comments

dave reid’s picture

Status: Active » Closed (duplicate)