Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezfile.php
Go to the documentation of this file.
1 <?php
17 class eZFile
18 {
24 const READ_PACKET_SIZE = 16384;
25
31 const CLEAN_ON_FAILURE = 1,
32 APPEND_DEBUG_ON_FAILURE = 2;
33
41 static function create( $filename, $directory = false, $data = false, $atomic = false )
42 {
43 $filepath = $filename;
44 if ( $directory )
45 {
46 if ( !file_exists( $directory ) )
47 {
48 eZDir::mkdir( $directory, false, true );
49 // eZDebugSetting::writeNotice( 'ezfile-create', "Created directory $directory", 'eZFile::create' );
50 }
51 $filepath = $directory . '/' . $filename;
52 }
53 // If atomic creation is needed we will use a temporary
54 // file when writing the data, then rename it to the correct path.
55 if ( $atomic )
56 {
57 $realpath = $filepath;
58 $dirname = dirname( $filepath );
59 if ( strlen( $dirname ) != 0 )
60 $dirname .= "/";
61 $filepath = $dirname . "ezfile-tmp." . md5( $filepath . getmypid() . mt_rand() );
62 }
63
64 $file = fopen( $filepath, 'wb' );
65 if ( $file )
66 {
67 // eZDebugSetting::writeNotice( 'ezfile-create', "Created file $filepath", 'eZFile::create' );
68 if ( $data )
69 {
70 if ( is_resource( $data ) )
71 {
72 // block-copy source $data to new $file in 1MB chunks
73 while ( !feof( $data ) )
74 {
75 fwrite( $file, fread( $data, 1048576 ) );
76 }
77 fclose( $data );
78 }
79 else
80 fwrite( $file, $data );
81 }
82 fclose( $file );
83
84 if ( $atomic )
85 {
86 // If the renaming process fails, delete the temporary file
87 eZFile::rename( $filepath, $realpath, false, eZFile::CLEAN_ON_FAILURE );
88 }
89 return true;
90 }
91 // eZDebugSetting::writeNotice( 'ezfile-create', "Failed creating file $filepath", 'eZFile::create' );
92 return false;
93 }
94
102 static function suffix( $filename )
103 {
104 $parts = explode( '.', $filename);
105 return array_pop( $parts );
106 }
107
114 static function isWriteable( $filename )
115 {
116 if ( eZSys::osType() != 'win32' )
117 return is_writable( $filename );
118
119 /* PHP function is_writable() doesn't work correctly on Windows NT descendants.
120 * So we have to use the following hack on those OSes.
121 */
122 if ( !( $fd = @fopen( $filename, 'a' ) ) )
123 return FALSE;
124
125 fclose( $fd );
126
127 return TRUE;
128 }
129
152 static function rename( $srcFile, $destFile, $mkdir = false, $flags = 0 )
153 {
154 /* On windows we need to unlink the destination file first */
155 if ( strtolower( substr( PHP_OS, 0, 3 ) ) == 'win' )
156 {
157 @unlink( $destFile );
158 }
159 if( $mkdir )
160 {
161 eZDir::mkdir( dirname( $destFile ), false, true );
162 }
163
164 $status = rename( $srcFile, $destFile );
165 // Rename operation failed, check $flags to know what to do then
166 if ( $status === false )
167 {
168 if ( $flags & self::APPEND_DEBUG_ON_FAILURE )
169 eZDebug::writeWarning( "$srcFile could not be renamed to $destFile", __METHOD__ );
170
171 if ( $flags & self::CLEAN_ON_FAILURE )
172 unlink( $srcFile );
173 }
174
175 return $status;
176 }
177
193 static function download( $file, $isAttachedDownload = true, $overrideFilename = false, $startOffset = 0, $length = false )
194 {
195 if ( !file_exists( $file ) )
196 {
197 return false;
198 }
199
200 ob_end_clean();
201 eZSession::stop();
202 self::downloadHeaders( $file, $isAttachedDownload, $overrideFilename, $startOffset, $length );
203 self::downloadContent( $file, $startOffset, $length );
204
205 eZExecution::cleanExit();
206 }
207
220 public static function downloadHeaders( $file, $isAttachedDownload = true, $overrideFilename = false, $startOffset = 0, $length = false, $fileSize = false )
221 {
222 if ( $fileSize === false )
223 {
224 if ( !file_exists( $file ) )
225 {
226 eZDebug::writeError( "\$fileSize not given, and file not found", __METHOD__ );
227 return false;
228 }
229
230 $fileSize = filesize( $file );
231 }
232
233 header( 'X-Powered-By: eZ Publish' );
234 $mimeinfo = eZMimeType::findByURL( $file );
235 header( "Content-Type: {$mimeinfo['name']}" );
236
237 // Fixes problems with IE when opening a file directly
238 header( "Pragma: " );
239 header( "Cache-Control: " );
240 // Last-Modified header cannot be set, otherwise browser like FF will fail while resuming a paused download
241 // because it compares the value of Last-Modified headers between requests.
242 header( "Last-Modified: " );
243 /* Set cache time out to 10 minutes, this should be good enough to work
244 around an IE bug */
245 header( "Expires: ". gmdate( 'D, d M Y H:i:s', time() + 600 ) . ' GMT' );
246 header(
247 "Content-Disposition: " .
248 ( $isAttachedDownload ? 'attachment' : 'inline' ) .
249 ( $overrideFilename !== false ? "; filename={$overrideFilename}" : '' )
250 );
251
252 // partial download (HTTP 'Range' header)
253 if ( $startOffset !== 0 )
254 {
255 $endOffset = ( $length !== false ) ? ( $length + $startOffset - 1 ) : $fileSize - 1;
256 header( "Content-Length: " . ( $endOffset - $startOffset + 1 ) );
257 header( "Content-Range: bytes {$startOffset}-{$endOffset}/{$fileSize}" );
258 header( "HTTP/1.1 206 Partial Content" );
259 }
260 else
261 {
262 header( "Content-Length: $fileSize" );
263 }
264 header( 'Content-Transfer-Encoding: binary' );
265 header( 'Accept-Ranges: bytes' );
266 }
267
277 public static function downloadContent( $file, $startOffset = 0, $length = false )
278 {
279 if ( !file_exists( $file ) )
280 {
281 eZDebug::writeError( "'$file' does not exist", __METHOD__ );
282 return false;
283 }
284 if ( ( $fp = fopen( $file, 'rb' ) ) === false )
285 {
286 eZDebug::writeError( "An error occured opening '$file' for reading", __METHOD__ );
287 return false;
288 }
289
290 $fileSize = filesize( $file );
291
292 // an offset has been given: move the pointer to that offset if it seems valid
293 if ( $startOffset !== false && $startOffset <= $fileSize && fseek( $fp, $startOffset ) === -1 )
294 {
295 eZDebug::writeError( "Error while setting offset on '{$file}'", __METHOD__ );
296 return false;
297 }
298
299 $transferred = $startOffset;
300 $packetSize = self::READ_PACKET_SIZE;
301 $endOffset = ( $length === false ) ? $fileSize - 1 : $length + $startOffset - 1;
302
303 while ( !feof( $fp ) && $transferred < $endOffset + 1 )
304 {
305 if ( $transferred + $packetSize > $endOffset + 1 )
306 {
307 $packetSize = $endOffset + 1 - $transferred;
308 }
309 echo fread( $fp, $packetSize );
310 $transferred += $packetSize;
311 }
312 fclose( $fp );
313
314 return true;
315 }
316 }
317
318 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZExecution\cleanExit
static cleanExit()
Definition: ezexecution.php:100
eZFile
Tool class which has convencience functions for files and directories.
Definition: ezfile.php:18
eZFile\CLEAN_ON_FAILURE
const CLEAN_ON_FAILURE
Definition: ezfile.php:31
eZFile\downloadContent
static downloadContent( $file, $startOffset=0, $length=false)
Definition: ezfile.php:277
eZFile\download
static download( $file, $isAttachedDownload=true, $overrideFilename=false, $startOffset=0, $length=false)
Definition: ezfile.php:193
eZFile\suffix
static suffix( $filename)
Definition: ezfile.php:102
eZFile\rename
static rename( $srcFile, $destFile, $mkdir=false, $flags=0)
Definition: ezfile.php:152
eZFile\downloadHeaders
static downloadHeaders( $file, $isAttachedDownload=true, $overrideFilename=false, $startOffset=0, $length=false, $fileSize=false)
Definition: ezfile.php:220
eZFile\READ_PACKET_SIZE
const READ_PACKET_SIZE
Definition: ezfile.php:24
eZFile\create
static create( $filename, $directory=false, $data=false, $atomic=false)
Definition: ezfile.php:41
eZFile\isWriteable
static isWriteable( $filename)
Definition: ezfile.php:114
eZFile\APPEND_DEBUG_ON_FAILURE
const APPEND_DEBUG_ON_FAILURE
Definition: ezfile.php:32
eZMimeType\findByURL
static findByURL( $url, $returnDefault=true)
Definition: ezmimetype.php:227
eZSys\osType
static osType()
Definition: ezsys.php:221
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.