欢迎各位兄弟 发布技术文章
这里的技术是共享的
在 Drupal 7 中,您可以通过以下几种方式获取公共文件系统路径(通常是 sites/default/files
):
php$public_path = variable_get('file_public_path', 'sites/default/files');
php$wrapper = file_stream_wrapper_get_instance_by_uri('public://'); $public_path = $wrapper->realpath();
php$public_path = drupal_realpath('public://');
php$public_url = file_create_url('public:/');
文件路径是在 Drupal 设置中配置的,所以不要直接硬编码 'sites/default/files'
如果要获取私有文件系统路径,使用 'private://' 代替 'public://'
在进行文件操作前,应该先检查目录是否存在且可写
使用示例:
php$public_path = variable_get('file_public_path', 'sites/default/files'); if (!file_prepare_directory($public_path, FILE_CREATE_DIRECTORY)) { drupal_set_message(t('公共文件目录不可写'), 'error'); }
这些方法都能可靠地获取 Drupal 7 中配置的公共文件系统路径。选择哪种方法取决于您的具体需求场景