By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,208 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

How do you delete a zip file from a directory?

P: n/a
Once again, I thought my class method deleteZip() would do the trick,
but it never deletes any .zip* file found in a directory:

[PHP]
/**
* Delete any latent ZIP files found in this album. This method is to
be inherited by all listing classes to allow for
* list-wide deletion of latent server-created ZIP files for security
purposes
*
* @access private
* @see actual_path
* @see is_class
*/
function &deleteZip() { // STATIC VOID METHOD
global $section, ${$section . 'LocationPath'}, $projectFullName;
$album = $_REQUEST['album'];
$locationPath = ($album) ? "${$section . 'LocationPath'}/$album" :
${$section . 'LocationPath'};
if (@!is_class($this->dbAP, 'DBActionPerformer')) $this->dbAP =& new
DBActionPerformer(); // LOCAL INSTANTIATION UNLESS ALREADY EXISTING
if (!$_ENV['windir']) { // UNIX VERSION
$statMsg = exec("stat \"" . actual_path($locationPath) . "/*.zip*\"
2>&1"); // USE UNIX CHECK 'stat' FOR LOCATING ANY ZIP FILES, DO
DELETION IF AT LEAST 1 FOUND
} else { // WINDOWS VERSION
list($lsKommand, $lsRedirect) =
@array_values($this->dbAP->getKommandOSArray('ls'));
$statMsg = exec("$lsKommand \"" . actual_path($locationPath) .
"/*.zip*\" $lsRedirect");
}
if (@!unlink(actual_path($locationPath) . '/*.zip*') &&
!preg_match('/no such file/i', $statMsg)) { // WEB SERVER CANNOT
DELETE ZIP FILES
list($removeKommand, $removeRedirect) =
@array_values($this->dbAP->getKommandOSArray('rmdir-force'));
$msg = exec("$removeKommand \"" . actual_path($locationPath) .
"/*.zip*\" $removeRedirect"); // ALLOW FOR THE SERVER ITSELF TO
DELETE THEM
if (preg_match('/^rm:/i', $msg) || ($_ENV['windir'] && $msg))
die("$projectFullName shut down due to security issue with
potentially damaging ZIP file in \"$locationPath\", please contact
administrator<p>Error: $msg");
}
}

[/PHP]

This is what happens:

statMsg = stat: cannot stat `/www/html/tools/app/images/doom/*.zip*':
No such file or directory
um, sorry but there *IS* at lease 1 zip file in
/www/html/tools/app/images/doom called images_of_doom.zip that is
auto-generated and does exist when I do "ls -l" via command line,
permissions of 0644.

How do I delete it and any other ZIP file, plain and simple, I thought
this would work and it doesn't, please help

thanx
phil

Oct 11 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
To expand a wildcard pattern, try the glob function. unlink isn't able
to handle wildcards. For instance:

$zips = glob("$theDir/*.zip");
if ($zips === false) {
die("glob failed.");
}
foreach ($zips as $zip) {
unlink($zip) or die("unlink failed.");
}

comp.lang.php wrote:
Once again, I thought my class method deleteZip() would do the trick,
but it never deletes any .zip* file found in a directory:

[PHP]
/**
* Delete any latent ZIP files found in this album. This method is to
be inherited by all listing classes to allow for
* list-wide deletion of latent server-created ZIP files for security
purposes
*
* @access private
* @see actual_path
* @see is_class
*/
function &deleteZip() { // STATIC VOID METHOD
global $section, ${$section . 'LocationPath'}, $projectFullName;
$album = $_REQUEST['album'];
$locationPath = ($album) ? "${$section . 'LocationPath'}/$album" :
${$section . 'LocationPath'};
if (@!is_class($this->dbAP, 'DBActionPerformer')) $this->dbAP =& new
DBActionPerformer(); // LOCAL INSTANTIATION UNLESS ALREADY EXISTING
if (!$_ENV['windir']) { // UNIX VERSION
$statMsg = exec("stat \"" . actual_path($locationPath) . "/*.zip*\"
2>&1"); // USE UNIX CHECK 'stat' FOR LOCATING ANY ZIP FILES, DO
DELETION IF AT LEAST 1 FOUND
} else { // WINDOWS VERSION
list($lsKommand, $lsRedirect) =
@array_values($this->dbAP->getKommandOSArray('ls'));
$statMsg = exec("$lsKommand \"" . actual_path($locationPath) .
"/*.zip*\" $lsRedirect");
}
if (@!unlink(actual_path($locationPath) . '/*.zip*') &&
!preg_match('/no such file/i', $statMsg)) { // WEB SERVER CANNOT
DELETE ZIP FILES
list($removeKommand, $removeRedirect) =
@array_values($this->dbAP->getKommandOSArray('rmdir-force'));
$msg = exec("$removeKommand \"" . actual_path($locationPath) .
"/*.zip*\" $removeRedirect"); // ALLOW FOR THE SERVER ITSELF TO
DELETE THEM
if (preg_match('/^rm:/i', $msg) || ($_ENV['windir'] && $msg))
die("$projectFullName shut down due to security issue with
potentially damaging ZIP file in \"$locationPath\", please contact
administrator<p>Error: $msg");
}
}

[/PHP]

This is what happens:

statMsg = stat: cannot stat `/www/html/tools/app/images/doom/*.zip*':
No such file or directory

um, sorry but there *IS* at lease 1 zip file in
/www/html/tools/app/images/doom called images_of_doom.zip that is
auto-generated and does exist when I do "ls -l" via command line,
permissions of 0644.

How do I delete it and any other ZIP file, plain and simple, I thought
this would work and it doesn't, please help

thanx
phil
Oct 11 '06 #2

P: n/a

pe*******@gmail.com wrote:
To expand a wildcard pattern, try the glob function. unlink isn't able
to handle wildcards. For instance:

$zips = glob("$theDir/*.zip");
if ($zips === false) {
die("glob failed.");
}
foreach ($zips as $zip) {
unlink($zip) or die("unlink failed.");
}
Thanx but how efficient is that? You could be looping through a
directory potentially containing thousands of images within my
application each time you display a list of them

>
comp.lang.php wrote:
Once again, I thought my class method deleteZip() would do the trick,
but it never deletes any .zip* file found in a directory:

[PHP]
/**
* Delete any latent ZIP files found in this album. This method is to
be inherited by all listing classes to allow for
* list-wide deletion of latent server-created ZIP files for security
purposes
*
* @access private
* @see actual_path
* @see is_class
*/
function &deleteZip() { // STATIC VOID METHOD
global $section, ${$section . 'LocationPath'}, $projectFullName;
$album = $_REQUEST['album'];
$locationPath = ($album) ? "${$section . 'LocationPath'}/$album" :
${$section . 'LocationPath'};
if (@!is_class($this->dbAP, 'DBActionPerformer')) $this->dbAP =& new
DBActionPerformer(); // LOCAL INSTANTIATION UNLESS ALREADY EXISTING
if (!$_ENV['windir']) { // UNIX VERSION
$statMsg = exec("stat \"" . actual_path($locationPath) . "/*.zip*\"
2>&1"); // USE UNIX CHECK 'stat' FOR LOCATING ANY ZIP FILES, DO
DELETION IF AT LEAST 1 FOUND
} else { // WINDOWS VERSION
list($lsKommand, $lsRedirect) =
@array_values($this->dbAP->getKommandOSArray('ls'));
$statMsg = exec("$lsKommand \"" . actual_path($locationPath) .
"/*.zip*\" $lsRedirect");
}
if (@!unlink(actual_path($locationPath) . '/*.zip*') &&
!preg_match('/no such file/i', $statMsg)) { // WEB SERVER CANNOT
DELETE ZIP FILES
list($removeKommand, $removeRedirect) =
@array_values($this->dbAP->getKommandOSArray('rmdir-force'));
$msg = exec("$removeKommand \"" . actual_path($locationPath) .
"/*.zip*\" $removeRedirect"); // ALLOW FOR THE SERVER ITSELF TO
DELETE THEM
if (preg_match('/^rm:/i', $msg) || ($_ENV['windir'] && $msg))
die("$projectFullName shut down due to security issue with
potentially damaging ZIP file in \"$locationPath\", please contact
administrator<p>Error: $msg");
}
}

[/PHP]

This is what happens:

statMsg = stat: cannot stat `/www/html/tools/app/images/doom/*.zip*':
No such file or directory
um, sorry but there *IS* at lease 1 zip file in
/www/html/tools/app/images/doom called images_of_doom.zip that is
auto-generated and does exist when I do "ls -l" via command line,
permissions of 0644.

How do I delete it and any other ZIP file, plain and simple, I thought
this would work and it doesn't, please help

thanx
phil
Oct 11 '06 #3

P: n/a
"comp.lang.php" <ph**************@gmail.comwrote:
>
pe*******@gmail.com wrote:
>To expand a wildcard pattern, try the glob function. unlink isn't able
to handle wildcards. For instance:

$zips = glob("$theDir/*.zip");
if ($zips === false) {
die("glob failed.");
}
foreach ($zips as $zip) {
unlink($zip) or die("unlink failed.");
}

Thanx but how efficient is that? You could be looping through a
directory potentially containing thousands of images within my
application each time you display a list of them
You shouldn't worry too much about efficiency until you try it. Try doing
an "ls *.zip" from a command line; the "glob" is going to take roughly the
same time. The Unix directory APIs are pretty efficient.

On the other hand, once you get into "thousands" of images, you might
consider some clever subdirectories. I have a client that stores PDF and
JPG files, where the names have the form 'p12345.pdf'. I use mod_rewrite
to change that to a directory tree p1/23/p12345.pdf. That way, there are
no more than 100 files per directory.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 12 '06 #4

P: n/a

Tim Roberts wrote:
"comp.lang.php" <ph**************@gmail.comwrote:

pe*******@gmail.com wrote:
To expand a wildcard pattern, try the glob function. unlink isn't able
to handle wildcards. For instance:

$zips = glob("$theDir/*.zip");
if ($zips === false) {
die("glob failed.");
}
foreach ($zips as $zip) {
unlink($zip) or die("unlink failed.");
}
Thanx but how efficient is that? You could be looping through a
directory potentially containing thousands of images within my
application each time you display a list of them

You shouldn't worry too much about efficiency until you try it. Try doing
an "ls *.zip" from a command line; the "glob" is going to take roughly the
same time. The Unix directory APIs are pretty efficient.
Ok cool, however, the requirements for this portable web app is that it
must work in both Unix and Windows environments equally, thus,
obviously can't just do "ls *.zip" but also "dir /w *.zip" as well; how
does "glob" play with Windows?
>
On the other hand, once you get into "thousands" of images, you might
consider some clever subdirectories. I have a client that stores PDF and
JPG files, where the names have the form 'p12345.pdf'. I use mod_rewrite
to change that to a directory tree p1/23/p12345.pdf. That way, there are
no more than 100 files per directory.
-
clever subdirectories are an option inasmuch as PHP requires literal
paths for some of its functionality (I wrote a function "actual_path()"
that takes care of that anyway), however, as this is a portable web
application, I am not sure if that is a viable one, as this application
is designed to "pack up and go" and install anywhere on the planet (or
it should), one simply could not do mod_rewrite on the fly, only an
admin customizing my tool could do that on their end. Good to suggest
though

Phil

PS: I figured something out that might work for Unix/Windows:

[PHP]
if (!$_ENV['windir'] && !$_SERVER['windir']) {
$msg = exec('stat ' . actual_path($locationPath) . '/*.zip* 2>&1');
} else {
list($lsKommand, $lsRedirect) =
@array_values($this->getKommandOSArray('ls-l')); // GETS "ls" COMMAND
APPROPRIATE FOR NON-UNIX SYSTEMS
$msg = exec("$lsKommand \"" . actual_path($locationPath) . "/*.zip*\"
$lsRedirect");
}
[/PHP]
-
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 12 '06 #5

P: n/a
comp.lang.php wrote:
Tim Roberts wrote:

Ok cool, however, the requirements for this portable web app is that it
must work in both Unix and Windows environments equally, thus,
obviously can't just do "ls *.zip" but also "dir /w *.zip" as well; how
does "glob" play with Windows?
Why not use the directory functions, such as opendir(), readdir(), etc.?
They work on all systems. You'll have to test the file extensions,
but it shouldn't be too bad as the information is cached.

>

clever subdirectories are an option inasmuch as PHP requires literal
paths for some of its functionality (I wrote a function "actual_path()"
that takes care of that anyway), however, as this is a portable web
application, I am not sure if that is a viable one, as this application
is designed to "pack up and go" and install anywhere on the planet (or
it should), one simply could not do mod_rewrite on the fly, only an
admin customizing my tool could do that on their end. Good to suggest
though

Phil

PS: I figured something out that might work for Unix/Windows:

[PHP]
if (!$_ENV['windir'] && !$_SERVER['windir']) {
$msg = exec('stat ' . actual_path($locationPath) . '/*.zip* 2>&1');
} else {
list($lsKommand, $lsRedirect) =
@array_values($this->getKommandOSArray('ls-l')); // GETS "ls" COMMAND
APPROPRIATE FOR NON-UNIX SYSTEMS
$msg = exec("$lsKommand \"" . actual_path($locationPath) . "/*.zip*\"
$lsRedirect");
}
[/PHP]
-
>>- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.

$_SERVER['DOCUMENT_ROOT'] always points to the root directory of the
server, no matter where it is or what platform you're running Apache on
(it also works with IIS). From there is' a simple matter to append the
relative path from the document root to the directory you want.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 13 '06 #6

P: n/a

Jerry Stuckle wrote:
comp.lang.php wrote:
Tim Roberts wrote:

Ok cool, however, the requirements for this portable web app is that it
must work in both Unix and Windows environments equally, thus,
obviously can't just do "ls *.zip" but also "dir /w *.zip" as well; how
does "glob" play with Windows?

Why not use the directory functions, such as opendir(), readdir(), etc.?
They work on all systems. You'll have to test the file extensions,
but it shouldn't be too bad as the information is cached.



clever subdirectories are an option inasmuch as PHP requires literal
paths for some of its functionality (I wrote a function "actual_path()"
that takes care of that anyway), however, as this is a portable web
application, I am not sure if that is a viable one, as this application
is designed to "pack up and go" and install anywhere on the planet (or
it should), one simply could not do mod_rewrite on the fly, only an
admin customizing my tool could do that on their end. Good to suggest
though

Phil

PS: I figured something out that might work for Unix/Windows:

[PHP]
if (!$_ENV['windir'] && !$_SERVER['windir']) {
$msg = exec('stat ' . actual_path($locationPath) . '/*.zip* 2>&1');
} else {
list($lsKommand, $lsRedirect) =
@array_values($this->getKommandOSArray('ls-l')); // GETS "ls" COMMAND
APPROPRIATE FOR NON-UNIX SYSTEMS
$msg = exec("$lsKommand \"" . actual_path($locationPath) . "/*.zip*\"
$lsRedirect");
}
[/PHP]
-
>- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
$_SERVER['DOCUMENT_ROOT'] always points to the root directory of the
server, no matter where it is or what platform you're running Apache on
(it also works with IIS).
[snip]

That doesn't address the fact that your directory, stemming from
$_SERVER['DOCUMENT_ROOT'], could be "/var/www/html/blah/foo" or
"C:\Program Files\Apache Group\Apache\htdocs\blah\foo". Which is why I
will want to delete all of one type of file from a directory, the issue
lies in the fact that I am wanting to use command-line calls to remove
them all at one time (which honestly I thought was a time saver, but
honestly, is that slower or faster than your suggestion of opendir() -
readdir() while loop? I would think the while loop is slower as it has
to loop where a command-line remove command would be faster, but that's
just me), by using the * wildcard I cannot encase the path structure in
double-quotes, but in Windows, my directory from the doc root might
have spaces in it:

c:\Program Files\Apache Group\Apache\htdocs\My Directory\blah\foo

Why anyone would do that would be beyond me, but it is viable to occur
of course.

Also, for some strange reason, whenever I do this, it fails in spite of
the fact that $_SERVER['DOCUMENT_ROOT'] contains the path from the root
to the docroot:

[PHP]
list($removeKommand, $removeRedirect) =
@array_values($dbAP->getKommandOSArray('rmdir')); // GETS EITHER
WINDOWS OR NON-WINDOWS REMOVE COMMANDS AND REDIRECTION
$msg = exec("$removeKommand \"" . actual_path($_SERVER['DOCUMENT_ROOT']
.. '/blah/foo') . "/*.zip*\" $removeRedirect");
[/PHP]

Phil

Oct 13 '06 #7

P: n/a
comp.lang.php wrote:
Jerry Stuckle wrote:
<snip>
>>$_SERVER['DOCUMENT_ROOT'] always points to the root directory of the
server, no matter where it is or what platform you're running Apache on
(it also works with IIS).


[snip]

That doesn't address the fact that your directory, stemming from
$_SERVER['DOCUMENT_ROOT'], could be "/var/www/html/blah/foo" or
"C:\Program Files\Apache Group\Apache\htdocs\blah\foo". Which is why I
will want to delete all of one type of file from a directory, the issue
lies in the fact that I am wanting to use command-line calls to remove
them all at one time (which honestly I thought was a time saver, but
honestly, is that slower or faster than your suggestion of opendir() -
readdir() while loop? I would think the while loop is slower as it has
to loop where a command-line remove command would be faster, but that's
just me), by using the * wildcard I cannot encase the path structure in
double-quotes, but in Windows, my directory from the doc root might
have spaces in it:

c:\Program Files\Apache Group\Apache\htdocs\My Directory\blah\foo

Why anyone would do that would be beyond me, but it is viable to occur
of course.

Either way, $_SERVER['DOCUMENT_ROOT'] will point at your web root
directory. Just add your relative path from the root directory - which
the program should know already, anyway. It's just much easier to
reference the web server root directory all the time than to try to
compute the relative path. So you would just use:

$_SERVER['DOCUMENT_ROOT'] . '/blah/foo'

for your directory (yes, Windows understands forward slashes, also -
just the command processor doesn't).

And spaces are not a problem.

Using the PHP directory functions is going to be slower - but it is
transparent to the system. And many shared hosts do not allow PHP
programs to execute shell commands. This will always work, as long as
the files have the appropriate permissions (and if they don't, the shell
command will fail, also).
Also, for some strange reason, whenever I do this, it fails in spite of
the fact that $_SERVER['DOCUMENT_ROOT'] contains the path from the root
to the docroot:

[PHP]
list($removeKommand, $removeRedirect) =
@array_values($dbAP->getKommandOSArray('rmdir')); // GETS EITHER
WINDOWS OR NON-WINDOWS REMOVE COMMANDS AND REDIRECTION
$msg = exec("$removeKommand \"" . actual_path($_SERVER['DOCUMENT_ROOT']
. '/blah/foo') . "/*.zip*\" $removeRedirect");
[/PHP]

Phil
You don't need actual_path, for one thing. You already have it. And
not knowing what actually is passed in $removeKommand or
$removeRedirect, it's hard to say what happened.

Why not put your command into a string then echo it to see what's
actually in it? And the message that's returned.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 13 '06 #8

P: n/a
"comp.lang.php" <ph**************@gmail.comwrote:
>
Tim Roberts wrote:
>>
You shouldn't worry too much about efficiency until you try it. Try doing
an "ls *.zip" from a command line; the "glob" is going to take roughly the
same time. The Unix directory APIs are pretty efficient.

Ok cool, however, the requirements for this portable web app is that it
must work in both Unix and Windows environments equally, thus,
obviously can't just do "ls *.zip" but also "dir /w *.zip" as well;
I did not suggest that you actually use "ls *.zip" in your app. I
suggested that you do it ONE TIME and notice how quickly it runs, because
it uses essentially the same mechanism that "glob" will use. My purpose in
doing so was merely to reassure you that "glob" is not inefficient.
>how does "glob" play with Windows?
It works fine on Windows.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 14 '06 #9

P: n/a

Tim Roberts wrote:
"comp.lang.php" <ph**************@gmail.comwrote:

pe*******@gmail.com wrote:
To expand a wildcard pattern, try the glob function. unlink isn't able
to handle wildcards. For instance:

$zips = glob("$theDir/*.zip");
if ($zips === false) {
die("glob failed.");
}
foreach ($zips as $zip) {
unlink($zip) or die("unlink failed.");
}
Thanx but how efficient is that? You could be looping through a
directory potentially containing thousands of images within my
application each time you display a list of them

You shouldn't worry too much about efficiency until you try it. Try doing
an "ls *.zip" from a command line; the "glob" is going to take roughly the
same time. The Unix directory APIs are pretty efficient.
I wrote a function that will be used in case you don't have shell
access as a backup, that way, you will always have a way of performing
the same functionality whether you have shell access or not.

as far as "glob" goes, I checked here
http://us3.php.net/manual/en/function.glob.php under the first note,
and this portable application could be used to access and handle remote
files, thus "glob" will fail there (thus that's why I'm not using
"glob"):

[PHP]
if (!function_exists('move_all')) {
/**
* This function will perform the moving of all files from one
directory to another. Will return boolean as other PHP file/directory
commands will do as well
*
* "move_all()" will take the place of command-line "mv
/path/to/my/files/* /new/path/for/my/files" if the system environment
does not permit usage of command-line shell access. It will
* loop through the directory and use {@link
http://us3.php.net/manual/en/function.rename.php rename()} to move each
file individually
*
* @access public
* @param mixed $origPath
* @param mixed $newPath
* @return boolean
* @see actual_path
*/
function &move_all($oldPath, $newPath) {
if (!is_dir(actual_path($oldPath))) trigger_error("\"$oldPath\" is not
a valid source directory", E_USER_WARNING);
if (!is_dir(actual_path($newPath))) trigger_error("\"$newPath\" is not
a valid destination directory", E_USER_WARNING);
$dirID = @opendir($oldPath);
while (($fyl = @readdir($dirID)) !== false) if
(@!rename(actual_path("$oldPath/$fyl"), actual_path($newPath))) return
false;
@closedir($dirID);
return true;
}
}

[/PHP]

So thanx a lot for the suggestions as it totally helps keep this
portable web app being, well, more portable!
>
On the other hand, once you get into "thousands" of images, you might
consider some clever subdirectories. I have a client that stores PDF and
JPG files, where the names have the form 'p12345.pdf'. I use mod_rewrite
to change that to a directory tree p1/23/p12345.pdf. That way, there are
no more than 100 files per directory.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 19 '06 #10

P: n/a
comp.lang.php wrote:
>
if (@!is_class($this->dbAP, 'DBActionPerformer')) $this->dbAP =& new
DBActionPerformer(); // LOCAL INSTANTIATION UNLESS ALREADY EXISTING
I don't know about the actual unlinking of zip files, etc, but AFAIK
you can't create objects by reference. This should be:

....$this->dbAP = new DBActionPerformer();
Oct 24 '06 #11

P: n/a

Marcin Dobrucki wrote:
comp.lang.php wrote:

if (@!is_class($this->dbAP, 'DBActionPerformer')) $this->dbAP =& new
DBActionPerformer(); // LOCAL INSTANTIATION UNLESS ALREADY EXISTING

I don't know about the actual unlinking of zip files, etc, but AFAIK
you can't create objects by reference. This should be:

...$this->dbAP = new DBActionPerformer();
Um I'm sorry but yes you can. I've been doing this in PHP for years
with no problem:

$this->dbAP =& new DBActionPerformer();

Phil

Oct 24 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.