Public Member Functions | |
__construct ($fileId) | |
save ($destinationFolder, $newFileName=null) | |
correctFileNameCase ($fileName) | |
checkMimeType ($validTypes=Array()) | |
getUploadedFileName () | |
setAllowCreateFolders ($flag) | |
setAllowRenameFiles ($flag) | |
setFilesDispersion ($flag) | |
setFilenamesCaseSensitivity ($flag) | |
setAllowedExtensions ($extensions=array()) | |
chechAllowedExtension ($extension) | |
Static Public Member Functions | |
static | getCorrectFileName ($fileName) |
static | getNewFileName ($destFile) |
static | getDispretionPath ($fileName) |
Public Attributes | |
const | SINGLE_STYLE = 0 |
const | MULTIPLE_STYLE = 1 |
Static Protected Member Functions | |
static | _addDirSeparator ($dir) |
Protected Attributes | |
$_file | |
$_fileMimeType | |
$_uploadType | |
$_uploadedFileName | |
$_uploadedFileDir | |
$_allowCreateFolders = true | |
$_allowRenameFiles = false | |
$_enableFilesDispersion = false | |
$_caseInsensitiveFilenames = true | |
$_dispretionPath = null | |
$_fileExists = false | |
$_allowedExtensions = null |
Definition at line 35 of file Uploader.php.
__construct | ( | $ | fileId | ) |
Reimplemented in Varien_File_Uploader_Image.
Definition at line 127 of file Uploader.php.
00128 { 00129 $this->_setUploadFileId($fileId); 00130 if( !file_exists($this->_file['tmp_name']) ) { 00131 throw new Exception('File was not uploaded.'); 00132 return; 00133 } else { 00134 $this->_fileExists = true; 00135 } 00136 }
static _addDirSeparator | ( | $ | dir | ) | [static, protected] |
Definition at line 226 of file Uploader.php.
00227 { 00228 if (substr($dir,-1) != DIRECTORY_SEPARATOR) { 00229 $dir.= DIRECTORY_SEPARATOR; 00230 } 00231 return $dir; 00232 }
chechAllowedExtension | ( | $ | extension | ) |
Definition at line 321 of file Uploader.php.
00322 { 00323 if (is_null($this->_allowedExtensions)) { 00324 return true; 00325 } 00326 elseif (in_array(strtolower($extension), $this->_allowedExtensions)) { 00327 return true; 00328 } 00329 return false; 00330 }
checkMimeType | ( | $ | validTypes = Array() |
) |
Used to check if uploaded file mime type is valid or not
array | $validTypes public |
Definition at line 241 of file Uploader.php.
00242 { 00243 if( count($validTypes) > 0 ) { 00244 if( !in_array($this->_getMimeType(), $validTypes) ) { 00245 return false; 00246 } 00247 } 00248 return true; 00249 }
correctFileNameCase | ( | $ | fileName | ) |
Convert filename to lowercase in case of case-insensitive file names
string |
Definition at line 218 of file Uploader.php.
00219 { 00220 if ($this->_caseInsensitiveFilenames) { 00221 return strtolower($fileName); 00222 } 00223 return $fileName; 00224 }
static getCorrectFileName | ( | $ | fileName | ) | [static] |
Definition at line 203 of file Uploader.php.
00204 { 00205 if (preg_match('/[^a-z0-9_\\-\\.]/i', $fileName)) { 00206 $fileName = 'file' . substr($fileName, strrpos($fileName, '.')); 00207 } 00208 00209 return $fileName; 00210 }
static getDispretionPath | ( | $ | fileName | ) | [static] |
Definition at line 432 of file Uploader.php.
00433 { 00434 $char = 0; 00435 $dispretionPath = ''; 00436 while( ($char < 2) && ($char < strlen($fileName)) ) { 00437 if (empty($dispretionPath)) { 00438 $dispretionPath = DIRECTORY_SEPARATOR.('.' == $fileName[$char] ? '_' : $fileName[$char]); 00439 } 00440 else { 00441 $dispretionPath = self::_addDirSeparator($dispretionPath) . ('.' == $fileName[$char] ? '_' : $fileName[$char]); 00442 } 00443 $char ++; 00444 } 00445 return $dispretionPath; 00446 }
static getNewFileName | ( | $ | destFile | ) | [static] |
Definition at line 414 of file Uploader.php.
00415 { 00416 $fileInfo = pathinfo($destFile); 00417 if( file_exists($destFile) ) { 00418 $index = 1; 00419 $baseName = $fileInfo['filename'] . '.' . $fileInfo['extension']; 00420 while( file_exists($fileInfo['dirname'] . DIRECTORY_SEPARATOR . $baseName) ) { 00421 $baseName = $fileInfo['filename']. '_' . $index . '.' . $fileInfo['extension']; 00422 $index ++; 00423 } 00424 $destFileName = $baseName; 00425 } else { 00426 return $fileInfo['basename']; 00427 } 00428 00429 return $destFileName; 00430 }
getUploadedFileName | ( | ) |
save | ( | $ | destinationFolder, | |
$ | newFileName = null | |||
) |
Used to save uploaded file into destination folder with original or new file name (if specified)
string | $destinationFolder | |
string | $newFileName public |
Definition at line 147 of file Uploader.php.
00148 { 00149 if( $this->_fileExists === false ) { 00150 return; 00151 } 00152 00153 if( $this->_allowCreateFolders ) { 00154 $this->_createDestinationFolder($destinationFolder); 00155 } 00156 00157 if( !is_writable($destinationFolder) ) { 00158 throw new Exception('Destination folder is not writable or does not exists.'); 00159 } 00160 00161 $result = false; 00162 00163 $destFile = $destinationFolder; 00164 $fileName = ( isset($newFileName) ) ? $newFileName : self::getCorrectFileName($this->_file['name']); 00165 $fileExtension = substr($fileName, strrpos($fileName, '.')+1); 00166 00167 if( !$this->chechAllowedExtension($fileExtension) ) { 00168 throw new Exception('Disallowed file type.'); 00169 } 00170 00171 if( $this->_enableFilesDispersion ) { 00172 $fileName = $this->correctFileNameCase($fileName); 00173 $this->setAllowCreateFolders(true); 00174 $this->_dispretionPath = self::getDispretionPath($fileName); 00175 $destFile.= $this->_dispretionPath; 00176 $this->_createDestinationFolder($destFile); 00177 } 00178 00179 if( $this->_allowRenameFiles ) { 00180 $fileName = self::getNewFileName(self::_addDirSeparator($destFile).$fileName); 00181 } 00182 00183 $destFile = self::_addDirSeparator($destFile) . $fileName; 00184 00185 $result = move_uploaded_file($this->_file['tmp_name'], $destFile); 00186 00187 if( $result ) { 00188 chmod($destFile, 0777); 00189 if ( $this->_enableFilesDispersion ) { 00190 $fileName = str_replace(DIRECTORY_SEPARATOR, '/', self::_addDirSeparator($this->_dispretionPath)) . $fileName; 00191 } 00192 $this->_uploadedFileName = $fileName; 00193 $this->_uploadedFileDir = $destinationFolder; 00194 $result = $this->_file; 00195 $result['path'] = $destinationFolder; 00196 $result['file'] = $fileName; 00197 return $result; 00198 } else { 00199 return $result; 00200 } 00201 }
setAllowCreateFolders | ( | $ | flag | ) |
Used to set _allowCreateFolders value
mixed | $flag public |
Definition at line 269 of file Uploader.php.
setAllowedExtensions | ( | $ | extensions = array() |
) |
Definition at line 313 of file Uploader.php.
00314 { 00315 foreach ((array)$extensions as $extension) { 00316 $this->_allowedExtensions[] = strtolower($extension); 00317 } 00318 return $this; 00319 }
setAllowRenameFiles | ( | $ | flag | ) |
Used to set _allowRenameFiles value
mixed | $flag public |
Definition at line 282 of file Uploader.php.
setFilenamesCaseSensitivity | ( | $ | flag | ) |
Filenames Case-sensitivity setter
mixed | $flag |
Definition at line 307 of file Uploader.php.
setFilesDispersion | ( | $ | flag | ) |
Used to set _enableFilesDispersion value
mixed | $flag public |
Definition at line 295 of file Uploader.php.
$_allowCreateFolders = true [protected] |
Definition at line 85 of file Uploader.php.
$_allowedExtensions = null [protected] |
Definition at line 122 of file Uploader.php.
$_allowRenameFiles = false [protected] |
Definition at line 94 of file Uploader.php.
$_caseInsensitiveFilenames = true [protected] |
Definition at line 112 of file Uploader.php.
$_dispretionPath = null [protected] |
Definition at line 118 of file Uploader.php.
$_enableFilesDispersion = false [protected] |
Definition at line 102 of file Uploader.php.
$_file [protected] |
Definition at line 43 of file Uploader.php.
$_fileExists = false [protected] |
Definition at line 120 of file Uploader.php.
$_fileMimeType [protected] |
Definition at line 51 of file Uploader.php.
$_uploadedFileDir [protected] |
Definition at line 76 of file Uploader.php.
$_uploadedFileName [protected] |
Definition at line 68 of file Uploader.php.
$_uploadType [protected] |
Definition at line 59 of file Uploader.php.
const MULTIPLE_STYLE = 1 |
Definition at line 125 of file Uploader.php.
const SINGLE_STYLE = 0 |
Definition at line 124 of file Uploader.php.