51 $this->_mode = $writable ?
'w+b' :
'rb';
53 if (function_exists(
'get_magic_quotes_runtime') && @get_magic_quotes_runtime() == 1) {
54 $this->_quotes =
true;
82 public function read($length)
87 ini_set(
'magic_quotes_runtime', 0);
89 $bytes = fread($fp, $length);
91 ini_set(
'magic_quotes_runtime', 1);
93 $this->_offset = ftell($fp);
97 if ($bytes ===
'' && feof($fp)) {
120 if (isset($this->_reader)) {
123 $this->_offset = $byteOffset;
141 if (!isset($this->_reader)) {
142 if (!$this->_reader = fopen($this->_path,
'rb')) {
144 'Unable to open file for reading ['.$this->_path.
']'
147 if ($this->_offset != 0) {
159 if (!isset($this->_writer)) {
160 if (!$this->_writer = fopen($this->_path, $this->_mode)) {
162 'Unable to open file for writing ['.$this->_path.
']'
173 if (isset($this->_reader)) {
174 fclose($this->_reader);
175 $this->_reader = null;
182 $metas = stream_get_meta_data($this->_reader);
183 $this->_seekable = $metas[
'seekable'];
189 if ($this->_seekable === null) {
192 if ($this->_seekable ===
false) {
193 $currentPos = ftell($this->_reader);
194 if ($currentPos < $offset) {
195 $toDiscard = $offset - $currentPos;
196 fread($this->_reader, $toDiscard);
202 fseek($this->_reader, $offset, SEEK_SET);
208 if ($tmpFile = fopen(
'php://temp/maxmemory:4096',
'w+b')) {
210 }
elseif (function_exists(
'sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile())) {
213 throw new Swift_IoException(
'Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available');
215 $currentPos = ftell($this->_reader);
216 fclose($this->_reader);
217 $source = fopen($this->_path,
'rb');
219 throw new Swift_IoException(
'Unable to open file for copying ['.$this->_path.
']');
221 fseek($tmpFile, 0, SEEK_SET);
222 while (!feof($source)) {
223 fwrite($tmpFile, fread($source, 4096));
225 fseek($tmpFile, $currentPos, SEEK_SET);
227 $this->_reader = $tmpFile;