Varien_File_Uploader Class Reference

Inheritance diagram for Varien_File_Uploader:

Varien_File_Uploader_Image

List of all members.

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


Detailed Description

Definition at line 35 of file Uploader.php.


Constructor & Destructor Documentation

__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     }


Member Function Documentation

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

Parameters:
array $validTypes public
Returns:
bool

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

Parameters:
string 
Returns:
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 (  ) 

Returns a name of uploaded file

public

Returns:
string

Definition at line 257 of file Uploader.php.

00258     {
00259         return $this->_uploadedFileName;
00260     }

save ( destinationFolder,
newFileName = null 
)

Used to save uploaded file into destination folder with original or new file name (if specified)

Parameters:
string $destinationFolder
string $newFileName public
Returns:
void|bool

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

Parameters:
mixed $flag public
Returns:
Varien_File_Uploader

Definition at line 269 of file Uploader.php.

00270     {
00271         $this->_allowCreateFolders = $flag;
00272         return $this;
00273     }

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

Parameters:
mixed $flag public
Returns:
Varien_File_Uploader

Definition at line 282 of file Uploader.php.

00283     {
00284         $this->_allowRenameFiles = $flag;
00285         return $this;
00286     }

setFilenamesCaseSensitivity ( flag  ) 

Filenames Case-sensitivity setter

Parameters:
mixed $flag
Returns:
Varien_File_Uploader

Definition at line 307 of file Uploader.php.

00308     {
00309         $this->_caseInsensitiveFilenames = $flag;
00310         return $this;
00311     }

setFilesDispersion ( flag  ) 

Used to set _enableFilesDispersion value

Parameters:
mixed $flag public
Returns:
Varien_File_Uploader

Definition at line 295 of file Uploader.php.

00296     {
00297         $this->_enableFilesDispersion = $flag;
00298         return $this;
00299     }


Member Data Documentation

$_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.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:25:02 2009 for Magento by  doxygen 1.5.8