如何从文件字段中提取文件名和FilePath? -- 7 领域 和 entities 领域 和 hooks 领域 和 files 领域 drupal 相关 的问题

How do I extract filename and filepath from a file field?


简体版||繁體版
2
vote

问题

中文

这是我尝试解决的:我想允许通过默认的filefield上载内容类型上传zip文件。我正在使用hook_node_preSave拍摄zip文件,并从基于核心更新管理器的帮助函数中提取。我收到的错误是:

注意:未定义的索引:filefield_unzip_node_presave()中的文件名()(第18行) /sites/all/modules/filefield_unzip/filefield_unzip.module)。 注意:未定义的索引:filefield_unzip_node_presave()中的URI(第19行) /sites/all/modules/filefield_unzip/filefield_unzip.module)。 例外:归档者只能在本地文件上运行:> archiver_get_archiver()不支持(/包含/包含的行8149 /common.inc)。

这是我的代码:

  /** * @file  * Allows uploading and extracting a zip file uploaded to a file field. */  /**  *   * Implementation of hook_node_presave($node)  *   */  function filefield_unzip_node_presave($node) { if($node->type == 'game') {     if (!empty($node->field_game_files)) {         $zipFile = $node->field_game_files['und'][0]['filename'];         $zipDirectory = $node->field_game_files['und'][0]['uri'];          _filefield_unzip_archive_extract($zipFile, $zipDirectory);     } } }  /**  *   * Copy of the function used by the update manager update_manager_archive_extract($file, $directory)  *   */  function _filefield_unzip_archive_extract($file, $directory) { $archiver = archiver_get_archiver($file); if (!$archiver) {     throw new Exception(t('Cannot extract %file, not a valid archive.', array('%file' => $file))); }  //Remove the directory if it exists, otherwise it might contain a mixture of old files mixed with new files (e.g. in cases where files //were removed from a later release). $files = $archiver->listContents();  //Unfortunately, we can only use the direct name to determine the project name. Some archivers list the first file as the directory (i.e., MODULE/) //and others list an actual file (i.e., MODULE/README.TXT). $project = strtok($files[0], '/\');  $extract_location = $directory . '/' .$project; if (file_exists($extract_location)) {     file_unmanaged_delete_recursive($extract_location); }  $archiver->extract($directory); return $archiver; }   
english

Here is what I am trying to tackle: I would like to allow a zip file to be uploaded via the default filefield upload widget for a content type. I am using hook_node_presave to take the zip file and extract is using a helper function based off of the core update manager. The error I am receiving is:

Notice: Undefined index: filename in filefield_unzip_node_presave() (line 18 of /sites/all/modules/filefield_unzip/filefield_unzip.module). Notice: Undefined index: uri in filefield_unzip_node_presave() (line 19 of /sites/all/modules/filefield_unzip/filefield_unzip.module). Exception: Archivers can only operate on local files: >not supported in archiver_get_archiver() (line 8149 of /include /common.inc).

Here is my code:

/** * @file  * Allows uploading and extracting a zip file uploaded to a file field. */  /**  *   * Implementation of hook_node_presave($node)  *   */  function filefield_unzip_node_presave($node) { if($node->type == 'game') {     if (!empty($node->field_game_files)) {         $zipFile = $node->field_game_files['und'][0]['filename'];         $zipDirectory = $node->field_game_files['und'][0]['uri'];          _filefield_unzip_archive_extract($zipFile, $zipDirectory);     } } }  /**  *   * Copy of the function used by the update manager update_manager_archive_extract($file, $directory)  *   */  function _filefield_unzip_archive_extract($file, $directory) { $archiver = archiver_get_archiver($file); if (!$archiver) {     throw new Exception(t('Cannot extract %file, not a valid archive.', array('%file' => $file))); }  //Remove the directory if it exists, otherwise it might contain a mixture of old files mixed with new files (e.g. in cases where files //were removed from a later release). $files = $archiver->listContents();  //Unfortunately, we can only use the direct name to determine the project name. Some archivers list the first file as the directory (i.e., MODULE/) //and others list an actual file (i.e., MODULE/README.TXT). $project = strtok($files[0], '/\\');  $extract_location = $directory . '/' .$project; if (file_exists($extract_location)) {     file_unmanaged_delete_recursive($extract_location); }  $archiver->extract($directory); return $archiver; } 
           
 
 

回答列表

1
 
vote

我发现了一个解决方案,但是Stackexchange不会让我添加8小时的答案。我有一切键入,所以如果我忘记回来上传,这里会在这里发布,请求答案并提醒我。

继续...

 

I found a solution to this, however stackexchange won't let me add an answer for 8 hours. I have it all typed out so if I forget to come back and upload it feel free to post on here to request the answer and remind me.

go on...

 
 
 
 
1
 
vote

好的,所以经过一些额外的挖掘,我终于找到了解决方案。

1)对于与未定义索引有关的前两个误差...

事实证明,整个文件对象尚未在hook_node_preSave中加载。这就是未定义文件名和URI的原因。此时,唯一可以确保在对象上设置的是文件的FID,它位于数据库中的File_Managed表中,并引用包含其余元数据的特定字段表。对于如何通过FID抓取文件并加载整个东西以及将其元数据一起加载到脚本中的一个很好的解释,可以在此处找到:更改文件字段的文件(Drupal 7)

2)第二个问题只是我的误读。使用Archiver_Get_Archiver函数时需要提供文件的完整路径,而不仅仅是$ FILE PARAPATER中的文件名(我是在$目录参数中包含路径的印象),然后用于$目录参数您替换您实际上希望将文件解压缩的位置。

请参阅下面的更新代码:

  <?php  /**  * @file   * Allows uploading and extracting a zip file uploaded to a file field.  */  /**  *   * Implementation of hook_node_presave($node)  *   */  function filefield_unzip_node_presave($node) {     if($node->type == 'game') {     //get fid from file_managed table in the DB     $fid = $node->field_game_files[$node->language][0]['fid'];     //load the file associated with this fid     $file = file_load($fid);      //get the full path to the file     $zipFile = $file->uri;     //remove the filename from the directory     $zipDirectory = dirname($file->uri);      if (!empty($node->field_game_files)) {         _filefield_unzip_archive_extract($zipFile, $zipDirectory);     } } }  /**  *   * Copy of the function used by the update manager update_manager_archive_extract($file, $directory)  *   */  function _filefield_unzip_archive_extract($file, $directory) { $archiver = archiver_get_archiver($file); if (!$archiver) {     throw new Exception(t('Cannot extract %file, not a valid archive.', array('%file' => $file))); }  //Remove the directory if it exists, otherwise it might contain a mixture of old files mixed with new files (e.g. in cases where files //were removed from a later release). $files = $archiver->listContents();  //Unfortunately, we can only use the direct name to determine the project name. Some archivers list the first file as the directory (i.e., MODULE/) //and others list an actual file (i.e., MODULE/README.TXT). $project = strtok($files[0], '/\');  $extract_location = $directory . '/' .$project; if (file_exists($extract_location)) {     file_unmanaged_delete_recursive($extract_location); }  $archiver->extract($directory); return $archiver; }   
 

Okay, so after some additional digging I finally found the solution.

1) For the first two errors relating to undefined indexes...

It turns out that the entire file object is not loaded yet in hook_node_presave. This is why filename and uri are not defined. The only thing you can be sure has been set on the object at this point is the file's fid which is located in the file_managed table in the database and references the specific fields table which contains the rest of the metadata. A good explanation for how to grab the file by its fid and load the entire thing along with its metadata into your script can be found here: Changing the file of a file field (drupal 7)

2) The second problem was just a misread on my part. When using the archiver_get_archiver function you need to supply the full path to the file, not just the filename in the $file paramater (I was under the impression that the path was included in the $directory parameter), and then for the $directory parameter you substitute the location you actually want the file to be unzipped to.

Please see below for the updated code:

<?php  /**  * @file   * Allows uploading and extracting a zip file uploaded to a file field.  */  /**  *   * Implementation of hook_node_presave($node)  *   */  function filefield_unzip_node_presave($node) {     if($node->type == 'game') {     //get fid from file_managed table in the DB     $fid = $node->field_game_files[$node->language][0]['fid'];     //load the file associated with this fid     $file = file_load($fid);      //get the full path to the file     $zipFile = $file->uri;     //remove the filename from the directory     $zipDirectory = dirname($file->uri);      if (!empty($node->field_game_files)) {         _filefield_unzip_archive_extract($zipFile, $zipDirectory);     } } }  /**  *   * Copy of the function used by the update manager update_manager_archive_extract($file, $directory)  *   */  function _filefield_unzip_archive_extract($file, $directory) { $archiver = archiver_get_archiver($file); if (!$archiver) {     throw new Exception(t('Cannot extract %file, not a valid archive.', array('%file' => $file))); }  //Remove the directory if it exists, otherwise it might contain a mixture of old files mixed with new files (e.g. in cases where files //were removed from a later release). $files = $archiver->listContents();  //Unfortunately, we can only use the direct name to determine the project name. Some archivers list the first file as the directory (i.e., MODULE/) //and others list an actual file (i.e., MODULE/README.TXT). $project = strtok($files[0], '/\\');  $extract_location = $directory . '/' .$project; if (file_exists($extract_location)) {     file_unmanaged_delete_recursive($extract_location); }  $archiver->extract($directory); return $archiver; } 
 
 
1
 
vote

无法查看完整的代码, 但您可以更改和使用以下代码来获取文件路径,

$imgpath = file_load($ifid)->uri; print file_create_url($imgpath);

希望它有助于

 

Couldn't review your complete code, but you can alter and use below code to get file path,

$imgpath = file_load($ifid)->uri; print file_create_url($imgpath);

hope it helps

 
 

相关问题

1  WebForm,为上传文件创建自定义目录  ( Webform creating customized directories for uploaded files ) 
newbie和我在这里的第一个问题。我还在学习Drupal。如此,如果我错过了任何东西 这是我的问题 我正在使用webform来创建上传页面。用户输入ID(文本字段)并上载文件。该文件将上传到服务器上的私有部分。现在我试图确保文件上传到此单独的文件夹(以ID之后命名)(例如所有/私有/ id /)。我可以使用web...

2  Drupal的Multiboad Filefield小部件是否提供了表单API元素?  ( Does multiupload filefield widget for drupal provides a form api element ) 
我有一个表单在我的drupal页面上,使用multiboad filefield窗口小部件一次上传多个图像。 我想使用hook_form_alter从我的自定义模块改变此多层文件域窗口小部件的功能。 起初我认为它将是 managed_file 小部件。但它不是。 有没有办法做到这一点? ...

0  访问手机相机填充个人资料图片字段  ( Accessing the cell phone camera to populate the profile picture field ) 
我有一个移动网站,我认为如果用户用手机相机拍摄的方式填充该字段,而不是将它们上传他们手机上的图片,那么它会很酷类似地与某些网站使用的方式访问计算机的网络摄像头以拍摄照片。 我不确定这是否可以在手机上的Web浏览器中进行。我已经看到了通过浏览器访问计算机网络摄像头的Flash Souls,任何这些解决方案都适用于Dr...

1  是否是一个安全漏洞,用于验证上传的支持是zip文件作为zip_open()的zip?  ( Is it a security hole to verify an uploaded supposed to be zip file as a zip wit ) 
问题的主题几乎是它。 我在自定义内容类型上有一个上传小部件,用于提供zip文件。 我想验证他们提供的文件确实是一个zip文件,如果没有,则采取适当的动作。但是,首先,我需要知道是否安全地将未知的文件类型传递给zip_open(),可能使用zip_checkons标志以进行额外的完整性检查。 编辑:刚刚实现Zip...

6  有可能在D7中保存私人的私人私人拍摄图像样式(缩略图)?  ( Possible to make image styles thumbnails public while original images are kept ) 
我喜欢D7允许您选择文件是否应以每场基础存储在公共或私人文件系统中。 但这是我的问题:是否有可能具有存储在私有文件系统中的图像文件,而通过图像样式创建的缩略图等存储在公共文件系统,所以任何人都可以访问它们?换句话说,我想保护原始图像文件,但不是它们的缩略图。 默认为 theme_image_style 似乎放置了...

2  如何从文件字段中提取文件名和FilePath?  ( How do i extract filename and filepath from a file field ) 
这是我尝试解决的:我想允许通过默认的filefield上载内容类型上传zip文件。我正在使用hook_node_preSave拍摄zip文件,并从基于核心更新管理器的帮助函数中提取。我收到的错误是: 注意:未定义的索引:filefield_unzip_node_presave()中的文件名()(第18行) /s...

1  升级到PHP 7后如何修复文件上传  ( How do i fix file upload after upgrading to php 7 ) 
我知道这个问题已被问到一千种不同的方式,但这些解决方案都没有为我工作。 问题:我最近从php5更新到php7。每当我尝试上传图像(没有尝试任何其他文件类型)时,我都会得到附加的 我的设置: Drupal 8.6.1 PHP 7.2 Windows 2012服务器 iis 8 我尝试过: ...

23  在树枝中获取图像URL  ( Get image url in twig ) 
我想通过树枝中的内联样式渲染图像作为背景图像。 我创建了一个名为bg_image的字段,并将其附加到标准普通页面。 摆弄几个小时后,我能够在node.html.twig 中获取图像URL {{ file_url(node.field_bg_image.0.entity.uri.value) }} 但我无法将...

0  如何在Drupal 7中链接到PDF文件[已关闭]  ( How do i link to pdf file in drupal 7 ) 
关闭。这个问题需要详细信息或清晰度。它目前不接受答案。 想要改进这个问题?添加详细信息并阐明编辑此帖子的问题。 关闭 6年前。 ...

3  设置ropal-在弹出窗口中播放的视频  ( Setting up video for drupal to play in popup ) 
我已经设置了一个Drupal网站,并希望在用户点击图片图形时弹出视频框。听起来很简单,但事实证明它在Drupal,Flash,Codecs和iPhone的土地上并不直接......这就是我想要的: 用户点击我的Drupal网站上的链接,一个视频盒播放器弹出作为用户在PC或Mac中观看的迷你盒播放器。 对于iPhone...




© 2022 it.wenda123.org All Rights Reserved. 问答之家 版权所有