2

I'm moving some existing functionality into a new Drupal site.

  1. Files are uploaded via posted form
  2. Their file names are prefixes with UID
  3. They are then moved to temp holding directory
  4. A cron job on another server SCPs the files in batches
  5. The files are verified copied, then deleted via batch ssh command

I'm looking for a clean way to handle (save) an uploaded file coming from a submitted form with an element of type 'file' without "managing" it. I've browsed over file_save_upload() and like the validation/error handling it offers.

However, I'd rather not have the files exist in the managed file tables. I would need to modify my pulling server's cron job to trigger a managed deletes via web request.

Just as there is a file_save_data() and analogous file_unmanaged_save_data() I'm looking for file_unmanaged_save_upload(). I basically want the first half of file_save_upload() before it touch the database. It seems like something similar or a workaround should already exist but I haven't found one. I trying to avoid reinventing the wheel.

  • Not sure about the #4 and #5, but if you still need Drupal to handle the uploaded files (from new SCP'd location), you will need to use file_save_upload. Once the files are copied, update the file location to the new one (or go one step ahead and add a new stream wrapper). – AyeshK May 5 '14 at 16:54
1

I suggest using file_save_upload() anyway, beacause (as the API docs say):

The file will be added to the {file_managed} table as a temporary file. Temporary files are periodically cleaned. To make the file a permanent file, assign the status and use file_save() to save the changes.

So you don't need to worry about having the file "managed" by Drupal as long as you don't change the status of the file to FILE_STATUS_PERMANENT.

Otherwise, if you really don't want this default behaviour (and also no hooks to get invoked), just copy the file in your form_submit() using file_unmanaged_copy() (give it uploaded tmp file as source).

1

You may create your own file_unmanaged_save_upload() which won't insert item into Drupal DB file_managed table.

I did. I copied file_save_upload() as file_unmanaged_save_upload() in /Drupal_root/includes/file.inc, and deleted "file_save()" in it, modify the last lines as below:

function file_unmanaged_save_upload(){
.....
if ($file = file_save($file)) {
    // Add file to the cache.
    $upload_cache[$source] = $file;
    return $file;
  }
  return FALSE;
}

It works as expected.

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.