Permalink
Browse files

Merge pull request #7 from wrep/master

Prevent overwriting/deletion of files when multiple passes are generated at the same time
  • Loading branch information...
2 parents 8857fd3 + 064d675 commit 59fb1e4d982d8ec24c7f18cc6890208dc1e3a928 @tschoffelen committed Sep 21, 2012
Showing with 26 additions and 7 deletions.
  1. +26 −7 PKPass.php
View
@@ -63,9 +63,14 @@ class PKPass {
* Holds error info if an error occured
*/
private $sError = '';
-
-
-
+
+ /*
+ * Holds a autogenerated uniqid to prevent overwriting other processes pass files
+ */
+ private $uniqid = null;
+
+
+
#################################
#########PUBLIC FUNCTIONS########
#################################
@@ -161,7 +166,7 @@ public function create($output = false) {
}
// Check if pass is created and valid
- if(!file_exists($this->tempPath.'pass.pkpass') || filesize($this->tempPath.'pass.pkpass') < 1){
+ if(!file_exists($paths['pkpass']) || filesize($paths['pkpass']) < 1) {
$this->sError = 'Error while creating pass.pkpass. Check your Zip extension.';
$this->clean();
return false;
@@ -324,10 +329,19 @@ protected function paths() {
if(substr($this->tempPath, -1) != '/') {
$this->tempPath = $this->tempPath.'/';
}
-
+
+ //Generate a unique subfolder in the tempPath to support generating more passes at the same time without erasing/overwriting each others files
+ if (empty($this->uniqid)) {
+ $this->uniqid = uniqid('PKPass', true);
+
+ if (!is_dir($this->tempPath.$this->uniqid)) {
+ mkdir($this->tempPath.$this->uniqid);
+ }
+ }
+
//Add temp folder path
foreach($paths AS $pathName => $path) {
- $paths[$pathName] = $this->tempPath.$path;
+ $paths[$pathName] = $this->tempPath.$this->uniqid.'/'.$path;
}
return $paths;
@@ -344,7 +358,12 @@ protected function clean() {
unlink($path);
}
}
-
+
+ //Remove our unique temporary folder
+ if (is_dir($this->tempPath.$this->uniqid)) {
+ rmdir($this->tempPath.$this->uniqid);
+ }
+
return true;
}
}

0 comments on commit 59fb1e4

Please sign in to comment.