DrupalBin
Submit Code
About
Recent Posts
Fix for Latest item + title list
Support Ticketing System module: code excerpt from 6.x-1.x-dev support.module lines 2843 to 2941
Fix for Create product reference field
Create product reference field
Re:
Fix for Code
Code
Code
bellHead's fields error
Code
more
User login
Log in using OpenID:
What is OpenID?
Username:
*
Password:
*
Log in using OpenID
Cancel OpenID login
Create new account
Request new password
Tags
CCK
drupal
fapi
jquery
menu
module
php
simpletest
test
theme
user
views
more tags
Home
Fix for This code will extract files form a zip file uploaded via a specific FileField
View
Fix
May 31, 2010 - 1:38am — Anonymous
<?php
function
my_unzip_nodeapi
(
&
$node
,
$op
,
$a3
=
NULL
,
$a4
=
NULL
)
{
if
(
$node
-
>
type
==
'test'
)
{
dsm
(
$node
)
;
switch
(
$op
)
{
case
'update'
:
if
(
!
is_null
(
$node
-
>
field_zipfile
[
0
]
)
)
{
dsm
(
'a zip file is atttached.'
)
;
my_unzip_unzip
(
$node
)
;
}
else
{
dsm
(
'No zip file is attached.'
)
;
}
break
;
}
// /switch ($op)
}
}
function
my_unzip_unzip
(
$node
)
{
global
$base_path
;
$zipfilepath
=
$node
-
>
field_zipfile
[
0
]
[
'filepath'
]
;
$unzipdir
=
file_directory_path
(
)
.
'/nid_'
.
$node
-
>
nid
.
'/unzipfiles'
;
$macdir_in_unzipdir
=
$unzipdir
.
'/__MACOSX'
;
if
(
!
file_exists
(
$unzipdir
)
)
{
mkdir
(
$unzipdir
)
;
}
$zip
=
new
ZipArchive;
$res
=
$zip
-
>
open
(
$zipfilepath
)
;
if
(
$res
===
TRUE
)
{
$zip
-
>
extractTo
(
$unzipdir
)
;
$zip
-
>
close
(
)
;
dsm
(
'The attached zip file was decompressed successfully.'
)
;
}
else
{
dsm
(
'The decompression was failed.'
)
;
}
if
(
file_exists
(
$macdir_in_unzipdir
)
)
{
deleteDirectory
(
$macdir_in_unzipdir
)
;
}
}
function
deleteDirectory
(
$dir
)
{
if
(
!
file_exists
(
$dir
)
)
return
true
;
if
(
!
is_dir
(
$dir
)
)
return
unlink
(
$dir
)
;
foreach
(
scandir
(
$dir
)
as
$item
)
{
if
(
$item
==
'.'
||
$item
==
'..'
)
continue
;
if
(
!
deleteDirectory
(
$dir
.DIRECTORY_SEPARATOR.
$item
)
)
return
false
;
}
return
rmdir
(
$dir
)
;
}
filefield
unzip
Submit Fix
Summary:
Tags:
Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Show summary in full view
<?php function my_unzip_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { if ($node->type == 'test') { dsm($node); switch ($op) { case 'update': if (!is_null($node->field_zipfile[0])) { dsm('a zip file is atttached.'); my_unzip_unzip($node); } else { dsm('No zip file is attached.'); } break; } // /switch ($op) } } function my_unzip_unzip($node) { global $base_path; $zipfilepath = $node->field_zipfile[0]['filepath']; $unzipdir = file_directory_path() . '/nid_' . $node->nid . '/unzipfiles'; $macdir_in_unzipdir = $unzipdir . '/__MACOSX'; if (!file_exists($unzipdir)){ mkdir($unzipdir); } $zip = new ZipArchive; $res = $zip->open($zipfilepath); if ($res === TRUE) { $zip->extractTo($unzipdir); $zip->close(); dsm('The attached zip file was decompressed successfully.'); } else { dsm('The decompression was failed.'); } if (file_exists($macdir_in_unzipdir)){ deleteDirectory($macdir_in_unzipdir); } } function deleteDirectory($dir) { if (!file_exists($dir)) return true; if (!is_dir($dir)) return unlink($dir); foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') continue; if (!deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false; } return rmdir($dir); }
Syntax highlighting mode:
ActionScript
ColdFusion
Diff
Drupal 5
Drupal 6
HTML
INI
Javascript
MySQL
PHP
Python
robots.txt
SQL
Text
Select the syntax highlighting mode to use.
See Also:
Order
Title:
URL:
-1
0
1
Title:
URL:
-1
0
1
Any links you'd like to have associated with the post (Drupal.org issue, Wikipedia article, etc).
File attachments
Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.
Attach new file:
The maximum upload size is
1 MB
. Only files with the following extensions may be uploaded:
jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp
.