Need help processing a ZIP file
This is my scenario:
- I upload a zip file
- Check each file in zip file
- If file != image, then move file to destination
- if file == image, resize image and move to destination
I googled around and seen different solutions, but none where one can process files before saving them at the final destination.
This is the function I got so far:
// Extract zip file and return files in an array.
private function processZip() {
$zip = new ZipArchive;
$tmp_dir = FB_PLUGIN_DIR.'tmp/';
// Extra开发者_StackOverflow社区ct files to tmp dir
if ($zip->open($this->file['tmp_name']) === TRUE) {
//Check if temp dir exists. If not, create one.
if (!is_dir($tmp_dir)) {
mkdir($tmp_dir, 0700);
}
$zip->extractTo($tmp_dir);
$zip->close();
/* Process extracted files */
foreach(glob($tmp_dir.'*.*') as $filename) {
// Somehow get MIME type here without using 'finfo' and 'mime_content_type'
// I haven't installed PEAR and 'mime_content_type' is decapricated.
}
return '1'; // success
} else {
return "0"; // fail
}
}
I'm not sure if I'm going in the right direction here. Somehow I think I should be able to process the files while in the "ZIP loop".
Is there a way I can read the files in the ZIP file, determin the MIME type and then process file?
I found this example: http://www.java-samples.com/showtutorial.php?tutorialid=985
I think it's close to what I need. But not sure what to modify.Decouple your processes. Extract everything from the ZIP file first, then scan the files for image files and process them.
It's a simpler process, and can be more easily decomposed for dealing with larger zipfiles.
Here's how I've processed a zip file in the past without extracting the whole thing
$zip = new ZipArchive;
$zip->open($file);
for ($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->statIndex($i);
if ($entry['size'] > 0) {
$data = $zip->getFromIndex($i);
// $data is a binary string of file data
// which can be used in GD functions for images
// and written to a location for regular files
if (preg_match('/\.jpe?g$/i', $entry['name'])) {
// JPEG file
} else {
// Not a JPEG file :-)
}
}
}
$zip->close();
Unfortunately, I couldn't find a way to actually read the file info without extracting it so I've gone with file extension detection.
精彩评论