setFileName ($file_name); } function getFileSize () { $result = 0; if (is_dir($this->getFileName ())) { foreach (new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($this->getFileName())) as $file) { $result += $file->getSize(); } } else { $result = filesize ($this->getFileName ()); } return $result; } function setFileName ($file_name) { $this->file_name = $file_name; } function getFileName () { return $this->file_name; } function open ($mode) { $this->unsetHandle (); switch (true) { case ($mode & self::OPEN_MODE_READWRITE) == self::OPEN_MODE_READWRITE: $file_mode = 'a+'; break; case ($mode & self::OPEN_MODE_APPEND): $file_mode = 'a'; break; case ($mode & self::OPEN_MODE_READ): $file_mode = 'r'; break; case ($mode & self::OPEN_MODE_WRITE): $file_mode = 'w'; if ($mode & self::OPEN_MODE_UNLINK_BEFORE_OPEN) { if (file_exists ($this->getFileName ())) { unlink ($this->getFileName ()); } } break; } $file = ''; if ($this->hasWrapper ()) { $file .= $this->getWrapper (); } $file .= $this->getFileName (); $file_handle = @fopen ($file, $file_mode); if ($file_handle === false) { throw new File_Exception('Cannot open file ' . $this->getFileName () . '.'); } $this->setHandle ($file_handle); } function read ($bytes_to_read = null) { if (!isset ($bytes_to_read)) { $bytes_to_read = $this->getBufferSize (); } $result = fread ($this->getHandle (), $bytes_to_read); if ($result === false) { throw new File_Exception('Cannot read from file ' . $this->getFileName () . '.'); } return $result; } function readLine () { $result = fgets ($this->getHandle ()); if (false === $result) { if (! feof ($this->getHandle ())) { throw new File_Exception ('Cannot read a line from file ' . $this->getFileName ()); } } return $result; } function write ($string) { $result = fwrite ($this->getHandle (), $string); if ($result === false) { throw new File_Exception('Cannot write to file ' . $this->getFileName () . '.'); } } function writeLine ($string = '') { $this->write ($string . PHP_EOL); } function copyTo ($filename) { if (is_object ($filename)) { $target = $filename; } else { $target = new File ($filename); } $target->open (File::OPEN_MODE_WRITE); $target->write ($this->getContents ()); return $target; } function getContents () { $save_error_level = error_reporting (0); $result = file_get_contents ($this->getFileName ()); error_reporting ($save_error_level); if ($result === false) { throw new File_Exception (sprintf ("Error uploading file %s", $this->getFileName ())); } return $result; } function getLines () { return file ($this->getFileName ()); } function close () { if ($this->hasHandle()) { $result = fclose ($this->getHandle ()); if ($result === false) { throw new File_Exception('Cannot close file ' . $this->getFileName () . '.'); } $this->unsetHandle (); } } function delete () { if ($this->hasHandle ()) { $this->close (); } $result = unlink ($this->getFileName ()); if ($result === false) { throw new File_Exception ('Cannot delete file ' . $this->getFileName () . '.'); } } function chmod ($mode) { $result = chmod ($this->getFileName (), $mode); if ($result === false) { throw new File_Exception ("Cannot change file mode."); } } function rename ($new_name) { $this->close (); $result = rename ($this->getFileName (), $new_name); if ($result === false) { throw new File_Exception (sprintf ("Cannot rename file %s to %s.", $this->getFileName (), $new_name)); } $this->setFileName ($new_name); } function isNfs () { return (boolean) preg_match ('/^\.nfs/', basename ($this->getFileName ())); } function setHandle ($file_handle) { $this->file_handle = $file_handle; } function getHandle () { return $this->file_handle; } function hasHandle () { return isset ($this->file_handle); } function unsetHandle () { unset ($this->file_handle); } function setBufferSize ($buffer_size) { $this->buffer_size = $buffer_size; } function getBufferSize () { return $this->buffer_size; } function setWrapper ($wrapper) { $this->wrapper = $wrapper; } function getWrapper () { return $this->wrapper; } function hasWrapper () { return isset($this->wrapper); } private $error_level; function setErrorLevel ($error_level) { $this->error_level = error_reporting (error_reporting () & ~$error_level); } function restoreErrorLevel () { if (isset ($this->error_level)) { error_reporting ($this->error_level); unset ($this->error_level); } } }