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;
}