Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

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