从Drupal 6 到 Drupal 7 文件API的改变(变更)

来源:互联网 发布:windows redmine 迁移 编辑:程序博客网 时间:2024/05/02 00:50

There are three key and pervasive things that happened to the File API between Drupal 6 and Drupal 7:

  1. Any API that used to take a traditional filepath like "sites/default/files/something.txt" now must take a stream-oriented file path like "public://something.txt" or "private://something.txt".
  2. Many APIs used to take a string if they were acting on an unmanaged file, or a file object if acting on a managed file. Now those functions have been split out, so we now have file_copy() (which takes a file object) and file_unmanaged_copy() (which takes a stream-oriented filepath like "public://example.txt").
  3. file_create_path() and file_check_directory() were merged into file_prepare_directory().
  4. A smaller item: drupal_realpath() must be used in place of realpath().

Summary of managed/unmanaged File API changes

Drupal 6Drupal 7Descriptionfile_copy()file_unmanaged_copy()Copy a file to a new location without saving a record in the database.n/afile_copy()Copies a file to a new location and adds a file record to the database. Also invokes hook_file_copy() so that other modules may act on the copy action.file_move()file_unmanaged_move()Move a file to a new location but make no changes to the database.n/afile_move()Move a file to a new location and update the file's database entry. Also invokes hook_file_move() so that other modules may act on the move action.file_delete()file_unmanaged_delete()Delete a file.n/afile_delete()Delete a file and its database record. Also invokes hook_file_delete() to let other modules perform clean-up actions when file is deleted.file_save_data()file_unmanaged_save_data()Save a string to the specified destination but makes no changes to the database.n/afile_save_data()Save a string to the specified destination and create a database file entry.n/afile_load()Load a file object from the database. Also invokes hook_file_load() to allow other modules to do things as the file is loaded.n/afile_validate()Check that a file meets the criteria specified by the validators. Accepts an associative array of callback functions used to validate the file. Also calls hook_file_validate() to let other modules perform validation on the new file.n/afile_save()Save a file object to the database. Calls either hook_file_insert() or hook_file_update(), depending on whether a $file->fid is specified.


file_create_path()file_prepare_directory(FILE_CREATE_DIRECTORY) file_check_directory()file_prepare_directory(FILE_MODIFY_PERMISSIONS)Probably most of the time people who just want to make sure a directory exists and is writable will use file_prepare_directory(FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)


原创粉丝点击