aboutsummaryrefslogtreecommitdiff
path: root/h-source/Library/Files/Upload.php
diff options
context:
space:
mode:
Diffstat (limited to 'h-source/Library/Files/Upload.php')
-rwxr-xr-xh-source/Library/Files/Upload.php104
1 files changed, 89 insertions, 15 deletions
diff --git a/h-source/Library/Files/Upload.php b/h-source/Library/Files/Upload.php
index 466582c..0c459ff 100755
--- a/h-source/Library/Files/Upload.php
+++ b/h-source/Library/Files/Upload.php
@@ -50,15 +50,18 @@ class Files_Upload
$this->pattern = "/^(".$tmp.")/";
$defaultParams = array(
- 'filesPermission'=>0777,
- 'delFolderAction'=>'delFolderAction',
- 'delFileAction'=>'delFileAction',
- 'createFolderAction'=>'createFolderAction',
- 'uploadFileAction'=>'uploadFileAction',
- 'maxFileSize' => 3000000,
- 'language' => 'Eng',
- 'allowedExtensions'=>'jpg,jpeg,png,gif,txt',
- 'fileUploadKey' => 'userfile'
+ 'filesPermission' => 0777,
+ 'delFolderAction' => 'delFolderAction',
+ 'delFileAction' => 'delFileAction',
+ 'createFolderAction' => 'createFolderAction',
+ 'uploadFileAction' => 'uploadFileAction',
+ 'maxFileSize' => 3000000,
+ 'language' => 'Eng',
+ 'allowedExtensions' => 'jpg,jpeg,png,gif,txt',
+ 'fileUploadKey' => 'userfile',
+ 'fileUploadBehaviour' => 'add_token', //can be none or add_token
+ 'fileUploadBeforeTokenChar' => '_',
+ 'functionUponFileNane' => 'none',
);
//set the $this->scaffold->params array
@@ -83,6 +86,15 @@ class Files_Upload
}
+ //set a new value for one element of the $params array
+ public function setParam($key,$value)
+ {
+ if (array_key_exists($key,$this->params))
+ {
+ $this->params[$key] = $value;
+ }
+ }
+
//change a resulting string
public function setString($key,$value)
{
@@ -206,6 +218,32 @@ class Files_Upload
return strtolower(end(explode('.', $file)));
}
+ //get the file name without the extension
+ protected function getNameWithoutFileExtension($file)
+ {
+ $copy = explode('.', $file);
+ array_pop($copy);
+ return implode('.',$copy);
+ }
+
+ //get a not existing file name if the one retrieved from the upload process already exists in the current directory
+ protected function getUniqueName($file,$int = 0)
+ {
+ $fileNameWithoutExt = $this->getNameWithoutFileExtension($file);
+ $extension = $this->getFileExtension($file);
+ $token = $int === 0 ? null : $this->params['fileUploadBeforeTokenChar'].$int;
+ $newName = $fileNameWithoutExt.$token.".$extension";
+ if (!file_exists($this->base.$this->directory.$newName))
+ {
+ return $newName;
+ }
+ else
+ {
+ return $this->getUniqueName($file,$int+1);
+ }
+
+ }
+
protected function parentDir() { #individuo la cartella madre
$folders = explode(self::DS,$this->directory);
@@ -389,17 +427,53 @@ class Files_Upload
return false;
}
+ //remove all the files that are not inside the $list argument
+ public function removeFilesNotInTheList($list = array())
+ {
+ $this->listFiles();
+ $files = $this->getFiles();
+ foreach ($files as $file)
+ {
+ if (!in_array($file,$list))
+ {
+ $this->removeFile($file);
+ }
+ }
+ }
+
//upload a file in the current directory
//$fileName: name of the file
- public function uploadFile($fileName = null) {
+ public function uploadFile($fileName = null)
+ {
$userfile = $this->params['fileUploadKey'];
- $ext = $this->getFileExtension($_FILES[$userfile]["name"]);
- $fileName = isset($fileName) ? $fileName.".$ext" : basename($_FILES[$userfile]["name"]);
-
- $this->fileName = $fileName;
if(strcmp(trim($_FILES[$userfile]["name"]),"") !== 0)
{
+ $nameFromUpload = basename($_FILES[$userfile]["name"]);
+
+ $ext = $this->getFileExtension($nameFromUpload);
+ $nameWithoutExtension = $this->getNameWithoutFileExtension($nameFromUpload);
+
+ if (!function_exists($this->params['functionUponFileNane'])) {
+ throw new Exception('Error in <b>'.__METHOD__.'</b>: function <b>'.$this->params['functionUponFileNane']. '</b> does not exist');
+ }
+
+ $nameWithoutExtension = call_user_func($this->params['functionUponFileNane'],$nameWithoutExtension);
+
+ $fileName = isset($fileName) ? $fileName.".$ext" : $nameWithoutExtension.".$ext";
+
+ $this->fileName = $fileName;
+
+ switch($this->params['fileUploadBehaviour'])
+ {
+ case 'none':
+ break;
+ case 'add_token':
+ $this->fileName = $this->getUniqueName($this->fileName);
+ $fileName = $this->fileName;
+ break;
+ }
+
if(@is_uploaded_file($_FILES[$userfile]["tmp_name"])) {
if ($_FILES[$userfile]["size"] <= $this->params['maxFileSize'])
{
@@ -408,7 +482,7 @@ class Files_Upload
if (in_array($ext,$AllowedExtensionsArray))
{
- //check if the file doesn't exists
+ //check if the file doesn't exist
if (!file_exists($this->base.$this->directory.$fileName))
{
if (@move_uploaded_file($_FILES[$userfile]["tmp_name"],$this->base.$this->directory.$fileName))