Reputation: 26281
I am using PHP to upload a zip file and extracting it. I was having problems with my validation script, and just discovered that the culprit was Thumbs.db
files which windows creates to improve caching. How can I delete these files prior to extracting?
Below is my attempt, but when extracting, other files have also been deleted. I've tried using both deleteIndex()
and deleteName()
, but get the same results. If I comment out the the line which deletes the Thumbs.db files, I do not experience the unintended deleted files. My syslog()
line only indicates that the Thumbs.db files are being deleted.
<?php
$zip = new ZipArchive();
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
//Validate files
$name=$zip->getNameIndex($i);
$pieces=explode(DIRECTORY_SEPARATOR,$name);
if($pieces[count($pieces)-1]=='Thumbs.db'){
//Delete windows created thumb.db files
syslog(LOG_INFO,'delete file '.$name.' with index '.$i);
$zip->deleteIndex($i); //If commented out, I do not experience the wrong files being deleted
//Also deletes wrong files: $zip->deleteName($name);
}
}
$zip->extractTo($myPath);
}
?>
Upvotes: 1
Views: 278
Reputation: 37018
Close the archive after file manipulations and reopen it before extracting:
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
...
}
$zip->close();
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
$zip->extractTo($myPath);
}
}
I believe in your example $zip->extractTo($myPath);
return false
.
In your particular case it may be simpler to use system utilities to unpack the archive and then delete the files with find.
Upvotes: 1