Varien_Io_File Class Reference

Inheritance diagram for Varien_Io_File:

Varien_Io_Abstract Varien_Io_Interface

List of all members.

Public Member Functions

 __destruct ()
 streamOpen ($fileName, $mode= 'w+', $chmod=0666)
 streamLock ($exclusive=true)
 streamUnlock ()
 streamRead ($length=1024)
 streamReadCsv ($delimiter= ',', $enclosure= '"')
 streamWrite ($str)
 streamClose ()
 streamStat ($part=null, $default=null)
 getStreamException ()
 open (array $args=array())
 setAllowCreateFolders ($flag)
 close ()
 mkdir ($dir, $mode=0777, $recursive=true)
 rmdir ($dir, $recursive=false)
 pwd ()
 cd ($dir)
 read ($filename, $dest=null)
 write ($filename, $src, $mode=null)
 fileExists ($file, $onlyFile=true)
 isWriteable ($path)
 getDestinationFolder ($filepath)
 createDestinationDir ($path)
 checkAndCreateFolder ($folder, $mode=0777)
 rm ($filename)
 mv ($src, $dest)
 cp ($src, $dest)
 chmod ($filename, $mode)
 ls ($grep=null)
 dirsep ()
 dirname ($file)

Public Attributes

const GREP_FILES = 'files_only'
const GREP_DIRS = 'dirs_only'

Protected Member Functions

 _parsePermissions ($mode)
 _getFileOwner ($filename)

Protected Attributes

 $_iwd
 $_cwd
 $_allowCreateFolders = false
 $_streamHandler
 $_streamFileName
 $_streamChmod
 $_streamLocked = false


Detailed Description

Definition at line 35 of file File.php.


Constructor & Destructor Documentation

__destruct (  ) 

Destruct

Definition at line 105 of file File.php.

00106     {
00107         if ($this->_streamHandler) {
00108             $this->streamClose();
00109         }
00110     }


Member Function Documentation

_getFileOwner ( filename  )  [protected]

Get file owner

Parameters:
string $filename protected
Returns:
string

Definition at line 764 of file File.php.

00765     {
00766         if( !function_exists('posix_getpwuid') ) {
00767             return 'n/a';
00768         }
00769 
00770         $owner     = posix_getpwuid(fileowner($filename));
00771         $groupinfo = posix_getgrnam(filegroup($filename));
00772 
00773         return $owner['name'] . ' / ' . $groupinfo;
00774     }

_parsePermissions ( mode  )  [protected]

Convert integer permissions format into human readable

Parameters:
integer $mode protected
Returns:
string

Definition at line 712 of file File.php.

00713     {
00714         if( $mode & 0x1000 )
00715             $type='p'; /* FIFO pipe */
00716         else if( $mode & 0x2000 )
00717             $type='c'; /* Character special */
00718         else if( $mode & 0x4000 )
00719             $type='d'; /* Directory */
00720         else if( $mode & 0x6000 )
00721             $type='b'; /* Block special */
00722         else if( $mode & 0x8000 )
00723             $type='-'; /* Regular */
00724         else if( $mode & 0xA000 )
00725             $type='l'; /* Symbolic Link */
00726         else if( $mode & 0xC000 )
00727             $type='s'; /* Socket */
00728         else
00729             $type='u'; /* UNKNOWN */
00730 
00731         /* Determine permissions */
00732         $owner['read'] = ($mode & 00400) ? 'r' : '-';
00733         $owner['write'] = ($mode & 00200) ? 'w' : '-';
00734         $owner['execute'] = ($mode & 00100) ? 'x' : '-';
00735         $group['read'] = ($mode & 00040) ? 'r' : '-';
00736         $group['write'] = ($mode & 00020) ? 'w' : '-';
00737         $group['execute'] = ($mode & 00010) ? 'x' : '-';
00738         $world['read'] = ($mode & 00004) ? 'r' : '-';
00739         $world['write'] = ($mode & 00002) ? 'w' : '-';
00740         $world['execute'] = ($mode & 00001) ? 'x' : '-';
00741 
00742         /* Adjust for SUID, SGID and sticky bit */
00743         if( $mode & 0x800 )
00744             $owner["execute"] = ($owner['execute']=='x') ? 's' : 'S';
00745         if( $mode & 0x400 )
00746             $group["execute"] = ($group['execute']=='x') ? 's' : 'S';
00747         if( $mode & 0x200 )
00748             $world["execute"] = ($world['execute']=='x') ? 't' : 'T';
00749 
00750         $s=sprintf('%1s', $type);
00751         $s.=sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
00752         $s.=sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
00753         $s.=sprintf('%1s%1s%1s', $world['read'], $world['write'], $world['execute']);
00754         return trim($s);
00755     }

cd ( dir  ) 

Change current working directory

Parameters:
string $dir
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 381 of file File.php.

00382     {
00383         if( is_dir($dir) ) {
00384             @chdir($this->_iwd);
00385             $this->_cwd = realpath($dir);
00386             return true;
00387         } else {
00388             throw new Exception('Unable to list current working directory.');
00389             return false;
00390         }
00391     }

checkAndCreateFolder ( folder,
mode = 0777 
)

Check and create if not exists folder

Parameters:
string $folder
int $mode
Returns:
bool

Definition at line 510 of file File.php.

00511     {
00512         if (is_dir($folder)) {
00513             return true;
00514         }
00515         if (!is_dir(dirname($folder))) {
00516             $this->checkAndCreateFolder(dirname($folder), $mode);
00517         }
00518         if (!is_dir($folder) && !@mkdir($folder, $mode)) {
00519             throw new Exception("Unable to create directory '{$folder}'. Access forbidden.");
00520         }
00521         return true;
00522     }

chmod ( filename,
mode 
)

Change mode of a directory or a file

Parameters:
string $filename
int $mode
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 614 of file File.php.

00615     {
00616         if ($this->_cwd) {
00617             chdir($this->_cwd);
00618         }
00619         $result = @chmod($filename, $mode);
00620         if ($this->_iwd) {
00621             chdir($this->_iwd);
00622         }
00623         return $result;
00624     }

close (  ) 

Close a connection

Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 305 of file File.php.

00306     {
00307         return true;
00308     }

cp ( src,
dest 
)

Copy a file

Parameters:
string $src
string $dest
Returns:
boolean

Definition at line 599 of file File.php.

00600     {
00601         @chdir($this->_cwd);
00602         $result = @copy($src, $dest);
00603         @chdir($this->_iwd);
00604         return $result;
00605     }

createDestinationDir ( path  ) 

Create destination folder

Parameters:
string $path
Returns:
Varien_Io_File

Definition at line 495 of file File.php.

00496     {
00497         if (!$this->_allowCreateFolders) {
00498             return false;
00499         }
00500         return $this->_createDestinationFolder($this->getCleanPath($path));
00501     }

dirname ( file  ) 

Definition at line 781 of file File.php.

00782     {
00783         return $this->getCleanPath(dirname($file));
00784     }

dirsep (  ) 

Retrieve directory separator in context of io resource

Reimplemented from Varien_Io_Abstract.

Definition at line 776 of file File.php.

00777     {
00778         return DIRECTORY_SEPARATOR;
00779     }

fileExists ( file,
onlyFile = true 
)

Definition at line 461 of file File.php.

00462     {
00463         @chdir($this->_cwd);
00464         $result = file_exists($file);
00465         if ($result && $onlyFile) {
00466             $result = is_file($file);
00467         }
00468         @chdir($this->_iwd);
00469         return $result;
00470     }

getDestinationFolder ( filepath  ) 

Definition at line 480 of file File.php.

00481     {
00482         preg_match('/^(.*[!\/])/', $filepath, $mathces);
00483         if (isset($mathces[0])) {
00484             return $mathces[0];
00485         }
00486         return false;
00487     }

getStreamException (  ) 

Retrieve stream methods exception

Returns:
Exception

Definition at line 258 of file File.php.

00259     {
00260         return $this->_streamException;
00261     }

isWriteable ( path  ) 

Definition at line 472 of file File.php.

00473     {
00474         @chdir($this->_cwd);
00475         $result = is_writeable($path);
00476         @chdir($this->_iwd);
00477         return $result;
00478     }

ls ( grep = null  ) 

Get list of cwd subdirectories and files

Suggestions (from moshe):

  • Use filemtime instead of filectime for performance
  • Change $grep to $flags and use binary flags
    • LS_DIRS = 1
    • LS_FILES = 2
    • LS_ALL = 3

Parameters:
Varien_Io_File const public
Returns:
array

Implements Varien_Io_Interface.

Definition at line 640 of file File.php.

00641     {
00642         $ignoredDirectories = Array('.', '..');
00643 
00644         if( is_dir($this->_cwd) ) {
00645             $dir = $this->_cwd;
00646         } elseif( is_dir($this->_iwd) ) {
00647             $dir = $this->_iwd;
00648         } else {
00649             throw new Exception('Unable to list current working directory.');
00650         }
00651 
00652         $list = Array();
00653 
00654         if ($dh = opendir($dir)) {
00655             while (($entry = readdir($dh)) !== false) {
00656                 $list_item = Array();
00657 
00658                 $fullpath = $dir . DIRECTORY_SEPARATOR . $entry;
00659 
00660                 if( ($grep == self::GREP_DIRS) && (!is_dir($fullpath)) ) {
00661                     continue;
00662                 } elseif( ($grep == self::GREP_FILES) && (!is_file($fullpath)) ) {
00663                     continue;
00664                 } elseif( in_array($entry, $ignoredDirectories) ) {
00665                     continue;
00666                 }
00667 
00668                 $list_item['text'] = $entry;
00669                 $list_item['mod_date'] = date ('Y-m-d H:i:s', filectime($fullpath));
00670                 $list_item['permissions'] = $this->_parsePermissions(fileperms($fullpath));
00671                 $list_item['owner'] = $this->_getFileOwner($fullpath);
00672 
00673                 if( is_file($fullpath) ) {
00674                     $pathinfo = pathinfo($fullpath);
00675                     $list_item['size'] = filesize($fullpath);
00676                     $list_item['leaf'] = true;
00677                     if( isset($pathinfo['extension']) && in_array(strtolower($pathinfo['extension']), Array('jpg', 'jpeg', 'gif', 'bmp', 'png')) && $list_item['size'] > 0 ) {
00678                         $list_item['is_image'] = true;
00679                         $list_item['filetype'] = $pathinfo['extension'];
00680                     } elseif( $list_item['size'] == 0 ) {
00681                         $list_item['is_image'] = false;
00682                         $list_item['filetype'] = 'unknown';
00683                     } elseif( isset($pathinfo['extension']) ) {
00684                         $list_item['is_image'] = false;
00685                         $list_item['filetype'] = $pathinfo['extension'];
00686                     } else {
00687                         $list_item['is_image'] = false;
00688                         $list_item['filetype'] = 'unknown';
00689                     }
00690                 } else {
00691                     $list_item['leaf'] = false;
00692                     $list_item['id'] = $fullpath;
00693                 }
00694 
00695                 $list[] = $list_item;
00696             }
00697             closedir($dh);
00698         } else {
00699             throw new Exception('Unable to list current working directory. Access forbidden.');
00700         }
00701 
00702         return $list;
00703     }

mkdir ( dir,
mode = 0777,
recursive = true 
)

Create a directory

Parameters:
string $dir
int $mode
boolean $recursive
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 318 of file File.php.

00319     {
00320         if ($this->_cwd) {
00321             chdir($this->_cwd);
00322         }
00323 
00324         $result = @mkdir($dir, $mode, $recursive);
00325         if ($result) {
00326             @chmod($dir, $mode);
00327         }
00328         if ($this->_iwd) {
00329             chdir($this->_iwd);
00330         }
00331         return $result;
00332     }

mv ( src,
dest 
)

Rename or move a directory or a file

Parameters:
string $src
string $dest
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 584 of file File.php.

00585     {
00586         chdir($this->_cwd);
00587         $result = @rename($src, $dest);
00588         chdir($this->_iwd);
00589         return $result;
00590     }

open ( array args = array()  ) 

Open a connection

Possible arguments:

  • path default current path

Parameters:
array $args
Returns:
boolean

Reimplemented from Varien_Io_Abstract.

Definition at line 272 of file File.php.

00273     {
00274         if (!empty($args['path'])) {
00275             if ($args['path']) {
00276                 if($this->_allowCreateFolders ) {
00277                     $this->_createDestinationFolder($args['path']);
00278                 }
00279             }
00280         }
00281 
00282         $this->_iwd = getcwd();
00283         $this->cd(!empty($args['path']) ? $args['path'] : $this->_iwd);
00284         return true;
00285     }

pwd (  ) 

Get current working directory

Returns:
string

Implements Varien_Io_Interface.

Definition at line 370 of file File.php.

00371     {
00372         return $this->_cwd;
00373     }

read ( filename,
dest = null 
)

Read a file to result, file or stream

If $dest is null the output will be returned. Otherwise it will be saved to the file or stream and operation result is returned.

Parameters:
string $filename
string|resource $dest
Returns:
boolean|string

Implements Varien_Io_Interface.

Definition at line 403 of file File.php.

00404     {
00405         if (!is_null($dest)) {
00406             chdir($this->_cwd);
00407             $result = @copy($filename, $dest);
00408             chdir($this->_iwd);
00409             return $result;
00410         }
00411 
00412         chdir($this->_cwd);
00413         $result = @file_get_contents($filename);
00414         chdir($this->_iwd);
00415 
00416         return $result;
00417     }

rm ( filename  ) 

Delete a file

Parameters:
string $filename
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 569 of file File.php.

00570     {
00571         @chdir($this->_cwd);
00572         $result = @unlink($filename);
00573         @chdir($this->_iwd);
00574         return $result;
00575     }

rmdir ( dir,
recursive = false 
)

Delete a directory

Parameters:
string $dir
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 340 of file File.php.

00341     {
00342         if( $this->_cwd ) {
00343             @chdir($this->_cwd);
00344         }
00345 
00346         if( $recursive ) {
00347             if( is_dir( $dir ) ){
00348                 foreach( scandir( $dir ) as $item ){
00349                     if( !strcmp( $item, '.' ) || !strcmp( $item, '..' ) )
00350                         continue;
00351                     $this->rmdir( $dir . "/" . $item, $recursive );
00352                 }
00353                 $result = @rmdir( $dir );
00354             } else {
00355                 $result = @unlink( $dir );
00356             }
00357         } else {
00358             $result = @rmdir($dir);
00359         }
00360 
00361         @chdir($this->_iwd);
00362         return $result;
00363     }

setAllowCreateFolders ( flag  ) 

Used to set _allowCreateFolders value

Parameters:
mixed $flag public
Returns:
void

Reimplemented from Varien_Io_Abstract.

Definition at line 294 of file File.php.

00295     {
00296         $this->_allowCreateFolders = $flag;
00297         return $this;
00298     }

streamClose (  ) 

Close an open file pointer Set chmod on a file

Returns:
bool

Definition at line 220 of file File.php.

00221     {
00222         if (!$this->_streamHandler) {
00223             return false;
00224         }
00225 
00226         if ($this->_streamLocked) {
00227             $this->streamUnlock();
00228         }
00229         @fclose($this->_streamHandler);
00230         @chmod($this->_streamFileName, $this->_streamChmod);
00231         return true;
00232     }

streamLock ( exclusive = true  ) 

Lock file

Returns:
bool

Definition at line 143 of file File.php.

00144     {
00145         if (!$this->_streamHandler) {
00146             return false;
00147         }
00148         $this->_streamLocked = true;
00149         $lock = $exclusive ? LOCK_EX : LOCK_SH;
00150         return flock($this->_streamHandler, $lock);
00151     }

streamOpen ( fileName,
mode = 'w+',
chmod = 0666 
)

Open file in stream mode For set folder for file use open method

Parameters:
string $fileName
string $mode
Returns:
bool

Definition at line 120 of file File.php.

00121     {
00122         $writeableMode = preg_match('#^[wax]#i', $mode);
00123         if ($writeableMode && !is_writeable($this->_cwd)) {
00124             throw new Exception('Permission denied for write to ' . $this->_cwd);
00125         }
00126         @chdir($this->_cwd);
00127         $this->_streamHandler = @fopen($fileName, $mode);
00128         @chdir($this->_iwd);
00129         if ($this->_streamHandler === false) {
00130             throw new Exception('Error write to file ' . $fileName);
00131         }
00132 
00133         $this->_streamFileName = $fileName;
00134         $this->_streamChmod = $chmod;
00135         return true;
00136     }

streamRead ( length = 1024  ) 

Binary-safe file read

Parameters:
int $length
Returns:
string

Definition at line 173 of file File.php.

00174     {
00175         if (!$this->_streamHandler) {
00176             return false;
00177         }
00178         if (feof($this->_streamHandler)) {
00179             return false;
00180         }
00181         return @fgets($this->_streamHandler, $length);
00182     }

streamReadCsv ( delimiter = ',',
enclosure = '"' 
)

Gets line from file pointer and parse for CSV fields

Returns:
string

Definition at line 189 of file File.php.

00190     {
00191         if (!$this->_streamHandler) {
00192             return false;
00193         }
00194         if (!ini_get('auto_detect_line_endings')) {
00195             ini_set('auto_detect_line_endings', 1);
00196         }
00197         return @fgetcsv($this->_streamHandler, 0, $delimiter, $enclosure);
00198     }

streamStat ( part = null,
default = null 
)

Retrieve open file statistic

Parameters:
string $part the part of statistic
mixed $default default value for part
Returns:
array|bool

Definition at line 241 of file File.php.

00242     {
00243         if (!$this->_streamHandler) {
00244             return false;
00245         }
00246         $stat = @fstat($this->_streamHandler);
00247         if (!is_null($part)) {
00248             return isset($stat[$part]) ? $stat[$part] : $default;
00249         }
00250         return $stat;
00251     }

streamUnlock (  ) 

Unlock file

Returns:
bool

Definition at line 158 of file File.php.

00159     {
00160         if (!$this->_streamHandler || !$this->_streamLocked) {
00161             return false;
00162         }
00163         $this->_streamLocked = false;
00164         return flock($this->_streamHandler, LOCK_UN);
00165     }

streamWrite ( str  ) 

Binary-safe file write

Parameters:
string $str
Returns:
bool

Definition at line 206 of file File.php.

00207     {
00208         if (!$this->_streamHandler) {
00209             return false;
00210         }
00211         return @fwrite($this->_streamHandler, $str);
00212     }

write ( filename,
src,
mode = null 
)

Write a file from string, file or stream

Parameters:
string $filename
string|resource $src
Returns:
int|boolean

Implements Varien_Io_Interface.

Definition at line 426 of file File.php.

00427     {
00428         if (is_string($src) && is_readable($src)) {
00429             $src = realpath($src);
00430             $srcIsFile = true;
00431         } elseif (is_string($src) || is_resource($src)) {
00432             $srcIsFile = false;
00433         } else {
00434             return false;
00435         }
00436         @chdir($this->_cwd);
00437 
00438         if (file_exists($filename)) {
00439             if (!is_writeable($filename)) {
00440                 printf('File %s don\'t writeable', $filename);
00441                 return false;
00442             }
00443         } else {
00444             if (!is_writable(dirname($filename))) {
00445                 printf('Folder %s don\'t writeable', dirname($filename));
00446                 return false;
00447             }
00448         }
00449         if ($srcIsFile) {
00450             $result = @copy($src, $filename);
00451         } else {
00452             $result = @file_put_contents($filename, $src);
00453         }
00454         if (!is_null($mode)) {
00455             @chmod($filename, $mode);
00456         }
00457         chdir($this->_iwd);
00458         return $result;
00459     }


Member Data Documentation

$_allowCreateFolders = false [protected]

Reimplemented from Varien_Io_Abstract.

Definition at line 72 of file File.php.

$_cwd [protected]

Definition at line 49 of file File.php.

$_iwd [protected]

Definition at line 42 of file File.php.

$_streamChmod [protected]

Definition at line 93 of file File.php.

$_streamFileName [protected]

Definition at line 86 of file File.php.

$_streamHandler [protected]

Definition at line 79 of file File.php.

$_streamLocked = false [protected]

Definition at line 100 of file File.php.

const GREP_DIRS = 'dirs_only'

Used to grep ls() output

Definition at line 63 of file File.php.

const GREP_FILES = 'files_only'

Used to grep ls() output

Definition at line 56 of file File.php.


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

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