原文地址:Working with files in Drupal 7 文件模块(FileField module)让你可以上传文件,管理文件(如果有合适的权限),并将其附加到内容上。此模块负责验证文件内容并管理上传的文件。它同样提供了显示文件内容的选项。 作为站点管理者你必须要能控制允许上传的文件类型以及文件的最大大小。 文件模块提主要通过一个File字段(通过Field module)来实现其功能。文件附件定义在内容类型这一层次上(或者其他的实体entities)。关于如何定义一个字段并将其添加到内容类型上,请参看Working with Field UI。 使用方法向内容类型添加一个文件字段打开一个内容类型的“管理字段”(Manage fields)页面。(Administer > Structure > Content types,点击你需要改变的内容类型对应的“管理字段”链接) 选择字段类型为文件(file),填上标签名和机读名(machine name),调整字段在所有字段中的位置,然后点击保存按钮。 
当你添加任何一个字段的时候,首先你需要设置这个字段特有的设置(见下图)。这些设置适用于所有使用了这个字段的地方(也可以说是全局设置,也就是不能根据每个用到此字段的实体entity或者说内容类型来做修改) - Enable Display field选项,当被选中时,允许用户为每个文件选择是否显示在内容显示页面。(在内容编辑页面每个文件的下方会有一个选择框)
- Files displayed by default 决定每个文件默认的显示方式(仅当上一项启用的时候,此项方才有效)。
- 上传目的目录默认情况下只有公用文件(public files)可选择 -- 意味着文件都可以直接访问服务器得到(Drupal不会进行访问检查)。如果你需要使用私有文件访问,需要首先改变文件系统管理页面中的选项“管理>配置>媒体:文件系统”(Administer > Configuration > Media: File system)。下面有具体介绍

接下来则是针对当前用到这个字段的内容类型的设置,意味着这些设置在不同内容类型上可以是不同的。 
你可以为文件字段添加验证选项。必须有的选项包括,一个文件扩展名列表作为允许用户上传文件的限制。可选的文件上传目录(可以是在文件系统管理页面定义的文件夹的子文件夹)。你也可以指定一个每个文件允许的最大的尺寸。 
管理附件显示一旦文件被附加到内容上,你可以指定它是否显示在附加的文件列表中。多个文件将会自动显示在当前内容的下方;也可以选择在列表中不显示一些文件,而让它们嵌入(embed)显示在正文中需要的地方。(嵌入意味着你将文件路径复制并手动粘贴到内容正文中,或者考虑使用Insert模块来完成。这里还要注意“Fltered HTML”默认拒绝任何图像标签。) 
更多的管理文件列表显示的选项在对应内容类型的“管理显示”(Manage Display)标签页。 管理文件目录和访问权限当你创建了一个文件字段,你可以指定一个子目录作为某个内容类型的文件字段的上传路径。整个站点的文件系统路径设置在“管理>配置>媒体:文件系统" (Administer > Configuration > Media: File system)。 你也可以选择文件是存储在公共目录(public directory)或者私有文件存储区(private file storage area),在公共目录的文件可以直接通过web服务器访问;当公共文件被列出,链接可以让任何知道地址的人直接下载文件。在私有目录中的文件则不能通过web服务器直接下载;在私有文件列表中,链接是基于Drupal路径格式。这将会增加服务器负载与下载时间,因为Drupal必须针对每一次文件下载请求进行路径的解析,但这样一来将可以实现访问控制。 公共文件存储目录的最佳实践是像如下目录所示: sites/default/files 缺省的私有目录则是存储在如下的目录中: sites/default/files/private 你可以在文件系统配置页面设置私有文件目录,Drupal会自动创建相应的子目录并放置一个简单的.htaccess文件来阻止所有通过web服务器的访问。当然,请确保测试一下私有文件与目录的设置以保证它们确实不能被直接访问。 技术细节PHP 配置为了使文件上传能顺利工作,必须对PHP进行合理配置。下述PHP配置变量(位于php.ini,.htaccess或者settings.php中)供参考 - file_uploads = On 必须要设置为 "On"
- upload_max_filesize = 24M 不能大于 post_max_size
- max_input_time = 300 值太小则可能在上传大文件时失败
- memory_limit = 64M 值太小则可能在上传大文件时出现内存不足错误
- max_execution_time = 180 值太小则可能在上传大文件时失败
- post_max_size = 24M 限制上传到站点的输入的大小(包括文件的大小)
FileField with Progress Bar Note that the Progress Bar feature requires the PECL extension "uploadprogress". If it is not available, FileField will degrade to using the standard circle throbber instead to indicate activity.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FileFieldFileField provides a universal file upload field for CCK. It is a robust alternative to core's Upload module and an absolute must for users uploading a large number of files. Great for managing video and audio files for podcasts on your own site. Features - Configurable upload paths allow you to save files into per-field or per-user directories
- Per-field and per-node file size limits
- Extensive API for extending field widgets and managing files
- Full revision/translation file management
- Views support
- Ajax Uploads (and progress bar support with the PECL uploadprogress extension)
- Pretty Icons
- All the goodness of CCK (multiple output formats, shared fields, multiple values, and much more)
Requirements - PHP 5
- MimeDetect (required for Drupal 5, but highly recommended for Drupal 6)
Recommended modules - Transliteration: Converts characters in uploads to server-safe formats.
- ImageField: Upload widget for FileField that shows an image preview, with support for ALT and Title text.
- FileField Paths: Save files using node-based tokens.
- FileField Podcaster or iTunes: Create podcast feeds using Views and FileField.
- SWF Tools: A set of media players for playback of audio and video files.
- Search Files: Allows searching the actual contents of PDF, Word, Excel, text, and other files.
Tutorials: Upgrading to Drupal 7 #D7CX: FileField does not need a Drupal 7 version because it has been moved to Drupal Core! To upgrade FileField to Drupal 7, install the Drupal 7 CCK Package and use the Content Migrate to upgrade FileField to Drupal 7. |