Hello =)

the function "field_file_save_upload" in field_file.inc is responsible for uploading the files, that's clear. It's also possible to modify the uploaded files, that's really good.
But it would be very great if there's the ability to completely replace the file object, also if nothing has been uploaded: In this case, FALSE is returned immediately. But it would be easier for other module developer like me if I could introduce a file which has already been uploaded in another process or something else ==> So, it shouldn't be returned FALSE immediately, only at last, if file keeps empty, for example. Or another method to introduce an external file would be great.

Here a code example, so you can imagine:

Unchanged code from 6.x-3.0-beta3 (field_file.inc):

<?php
/**
 * Save a file upload to a new location. The source file is validated as a
 * proper upload and handled as such. By implementing hook_file($op = 'insert'),
 * modules are able to act on the file upload and to add their own properties
 * to the file.
 *
 * The file will be added to the files table as a temporary file. Temporary files
 * are periodically cleaned. To make the file permanent file call
 * file_set_status() to change its status.
 *
 * @param $source
 *   A string specifying the name of the upload field to save.
 * @param $validators
 *   An optional, associative array of callback functions used to validate the
 *   file. The keys are function names and the values arrays of callback
 *   parameters which will be passed in after the user and file objects. The
 *   functions should return an array of error messages, an empty array
 *   indicates that the file passed validation. The functions will be called in
 *   the order specified.
 * @param $dest
 *   A string containing the directory $source should be copied to. If this is
 *   not provided or is not writable, the temporary directory will be used.
 * @param $replace
 *   A boolean indicating whether an existing file of the same name in the
 *   destination directory should overwritten. A false value will generate a
 *   new, unique filename in the destination directory.
 * @return
 *   An array containing the file information, or 0 in the event of an error.
 */
function field_file_save_upload($source, $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_RENAME) {
  if (!$file = file_save_upload($source, $validators, $dest, $replace)) {
    return 0;
  }
  // Let modules add additional properties to the yet barebone file object.
  foreach (module_implements('file_insert') as $module) {
    $function =  $module .'_file_insert';
    $function($file);
  }
  _field_file_cache($file); // cache the file in order to minimize load queries
  return (array)$file;
}
?>

Modified code:

<?php
/**
 * Save a file upload to a new location. The source file is validated as a
 * proper upload and handled as such. By implementing hook_file($op = 'insert'),
 * modules are able to act on the file upload and to add their own properties
 * to the file.
 *
 * The file will be added to the files table as a temporary file. Temporary files
 * are periodically cleaned. To make the file permanent file call
 * file_set_status() to change its status.
 *
 * @param $source
 *   A string specifying the name of the upload field to save.
 * @param $validators
 *   An optional, associative array of callback functions used to validate the
 *   file. The keys are function names and the values arrays of callback
 *   parameters which will be passed in after the user and file objects. The
 *   functions should return an array of error messages, an empty array
 *   indicates that the file passed validation. The functions will be called in
 *   the order specified.
 * @param $dest
 *   A string containing the directory $source should be copied to. If this is
 *   not provided or is not writable, the temporary directory will be used.
 * @param $replace
 *   A boolean indicating whether an existing file of the same name in the
 *   destination directory should overwritten. A false value will generate a
 *   new, unique filename in the destination directory.
 * @return
 *   An array containing the file information, or 0 in the event of an error.
 */
function field_file_save_upload($source, $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_RENAME) {
  $file = file_save_upload($source, $validators, $dest, $replace);
  // Let modules add additional properties to the yet barebone file object.
  foreach (module_implements('file_insert') as $module) {
    $function =  $module .'_file_insert';
    $function($file);
  }

  if($file) { // a valid file object?
    _field_file_cache($file); // cache the file in order to minimize load queries
    return (array)$file;
  } else {
    return 0;
  }
}
?>

What do you think about it? Or is there still another way to introduce an external file? thx =)

Comments

grandcat’s picture

I'm sorry, the modified code was not the right one:

<?php
/**
* Save a file upload to a new location. The source file is validated as a
* proper upload and handled as such. By implementing hook_file($op = 'insert'),
* modules are able to act on the file upload and to add their own properties
* to the file.
*
* The file will be added to the files table as a temporary file. Temporary files
* are periodically cleaned. To make the file permanent file call
* file_set_status() to change its status.
*
* @param $source
*   A string specifying the name of the upload field to save.
* @param $validators
*   An optional, associative array of callback functions used to validate the
*   file. The keys are function names and the values arrays of callback
*   parameters which will be passed in after the user and file objects. The
*   functions should return an array of error messages, an empty array
*   indicates that the file passed validation. The functions will be called in
*   the order specified.
* @param $dest
*   A string containing the directory $source should be copied to. If this is
*   not provided or is not writable, the temporary directory will be used.
* @param $replace
*   A boolean indicating whether an existing file of the same name in the
*   destination directory should overwritten. A false value will generate a
*   new, unique filename in the destination directory.
* @return
*   An array containing the file information, or 0 in the event of an error.
*/
function field_file_save_upload($source, $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_RENAME) {
  $file = file_save_upload($source, $validators, $dest, $replace);
  // Let modules add additional properties to the yet barebone file object.
  foreach (module_implements('file_insert') as $module) {
    $function =  $module .'_file_insert';
    $file = $function($file);
  }

  if($file) { // a valid file object?
    _field_file_cache($file); // cache the file in order to minimize load queries
    return (array)$file;
  } else {
    return 0;
  }
}
?>
drewish’s picture

Status: Active » Fixed

please take a look at the field_file_save_file() in the latest -dev release. i think it will do what you're asking for.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.