PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Additional File Informations

Zend_File_Transfer can return additional information on files. The following methods are available:

  • getFileName($file = null, $path = true): This method will return the real file name of a transferred file.

  • getFileInfo($file = null): This method will return all internal information for the given file.

  • getFileSize($file = null): This method will return the real filesize for the given file.

  • getHash($hash = 'crc32', $files = null): This method returns a hash of the content of a given transferred file.

  • getMimeType($files = null): This method returns the mimetype of a given transferred file.

getFileName() accepts the name of the element as first parameter. If no name is given, all known filenames will be returned in an array. If the file is a multifile, you will also get an array. If there is only a single file a string will be returned.

By default file names will be returned with the complete path. If you only need the file name without path, you can set the second parameter, $path, which will truncate the file path when set to FALSE.

Example 401. Getting the Filename

<?php
$upload 
= new Zend_File_Transfer();
$upload->receive();

// Returns the file names from all files
$names $upload->getFileName();

// Returns the file names from the 'foo' form element
$names $upload->getFileName('foo');

Note

Note that the file name can change after you receive the file, because all filters will be applied once the file is received. So you should always call getFileName() after the files have been received.

getFileSize() returns per default the real filesize in SI notation which means you will get 2kB instead of 2048. If you need only the plain size set the useByteString option to FALSE.

Example 402. Getting the size of a file

<?php
$upload 
= new Zend_File_Transfer();
$upload->receive();

// Returns the sizes from all files as array if more than one file was uploaded
$size $upload->getFileSize();

// Switches of the SI notation to return plain numbers
$upload->setOptions(array('useByteString' => false));
$size $upload->getFileSize();

Client given filesize

Note that the filesize which is given by the client is not seen as save input. Therefor the real size of the file will be detected and returned instead of the filesize sent by the client.

getHash() accepts the name of a hash algorithm as first parameter. For a list of known algorithms refer to PHP's hash_algos method. If you don't specify an algorithm, the crc32 algorithm will be used by default.

Example 403. Getting the hash of a file

<?php
$upload 
= new Zend_File_Transfer();
$upload->receive();

// Returns the hashes from all files as array if more than one file was uploaded
$hash $upload->getHash('md5');

// Returns the hash for the 'foo' form element
$names $upload->getHash('crc32''foo');

Return value

Note that if the given file or form name contains more than one file, the returned value will be an array.

getMimeType() returns the mimetype of a file. If more than one file was uploaded it returns an array, otherwise a string.

Example 404. Getting the mimetype of a file

<?php
$upload 
= new Zend_File_Transfer();
$upload->receive();

$mime $upload->getMimeType();

// Returns the mimetype for the 'foo' form element
$names $upload->getMimeType('foo');

Client given mimetype

Note that the mimetype which is given by the client is not seen as save input. Therefor the real mimetype of the file will be detected and returned instead of the mimetype sent by the client.

Possible exception

Note that this method uses the fileinfo extension if it is available. If this extension can not be found, it uses the mimemagic extension. When no extension was found it raises an exception.

Original data within $_FILES

Due to security reasons also the original data within $_FILES will be overridden as soon as Zend_File_Transfer is initiated. When you want to omit this behaviour and have the original data simply set the detectInfos option to FALSE at initiation.

This option will have no effect after you initiated Zend_File_Transfer.

Zend Framework