WebFioriAPI ReferenceLearnDownloadContributeLearn{{result.parent_page}} > {{result.title}}{{result.title}}Classes{{result.class_name}}{{result.summary}}Methods{{result.name}}{{result.summary}}

namespace \webfiori\framework

class File

Class Attributes Summary

const MIME_TYPESAn associative array that contains MIME types of common files.

Class Methods Summary

public function __construct(string$fNameOrAbsPath, string|null$fPath)Creates new instance of the class.public function __toString()Returns JSON string that represents basic file info.public function append(string$data)Appends a string of data to the already existing data.public function getAbsolutePath()Returns the full path to the file.public function getChunks(int$chunkSize, string$encodeOrDecode)Split file raw data into chunks of fixed size.public function getDir()Returns the directory at which the file exist on.public function getExtension()Extract file extension from file name and return it.public function getFileMIMEType()Returns MIME type of the file.public function getID()Returns the ID of the file.public function getLastModified(string$format)Returns the time at which the file was last modified.public static function getMIMEType(string$ext)Returns MIME type of a file type.public function getName()Returns the name of the file.public function getPath()Returns the path of the file.public function getRawData(string$encodeOrDecode)Returns the raw data of the file.public function getSize()Returns the size of the file in bytes.public function isExist()Checks if the file exist or not.public static function isFileExist(string$path)Checks if file exist or not without throwing errors.public function read(int$from, int$to)Reads the file in binary mode.public function remove()Removes a file given its name and path.public function setDir(string$dir)Sets the name of the directory at which the file exist on.public function setId(string$id)Sets the ID of the file.public function setMIMEType(string$type)Sets the MIME type of the file.public function setName(string$name)Sets the name of the file (such as 'my-image.public function setPath(string$fPath)Sets the path of the file.public function setRawData(string$raw)Sets the binary representation of the file.public function toJSON()Returns a JSON string that represents the file.public function view(boolean$asAttachment)Display the file.public function write(boolean$append, boolean$create)Write raw binary data into a file.

Class Attributes Details

const MIME_TYPESAn associative array that contains MIME types of common files. As of version 1.1.2 of the class, the array contains the following MIME types:
  • Audio and Video Formats:
    • avi: video/avi
    • 3gp: video/3gpp
    • ogv: video/ogg
    • mp4: video/mp4
    • mov: video/quicktime
    • wmv: video/x-ms-wmv
    • flv: video/x-flv
    • mpeg: video/mpeg
    • midi: audio/midi
    • oga: audio/ogg
    • mp3: audio/mpeg
    • mid: audio/midi
    • wav: audio/aac
    • acc: audio/aac
  • Image Formats:
    • jpeg: image/jpeg
    • jpg: image/jpeg
    • png: image/png
    • bmp: image/bmp
    • ico: image/x-icon
    • tiff: image/tiff
    • svg: image/svg+xml
    • psd: image/vnd.adobe.photoshop
    • gif: image/gif
  • Documents Formats:
    • pdf: application/pdf
    • rtf: application/rtf
    • doc: application/msword
    • docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
    • ppt: application/vnd.ms-powerpoint
    • pptx: application/vnd.openxmlformats-officedocument.presentationml.presentation
    • xls: application/vnd.ms-excel
    • xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • Text Based Formats:
    • txt: text/plain
    • php: text/plain
    • log: text/plain
    • ini: text/plain
    • css: text/css
    • js: application/javascript
    • asm: text/x-asm
    • java: text/x-java-source
    • htaccess: application/x-extension-htaccess
    • asp: text/asp
    • c: text/x-c
    • cpp: text/x-c
    • csv: text/csv
    • htm: text/html
    • html: text/html
  • Other Formats:
    • sql: application/sql
    • jar: application/java-archive
    • zip: application/zip
    • rar: application/x-rar-compressed
    • tar: application/x-tar
    • 7z: application/x-7z-compressed
    • exe: application/vnd.microsoft.portable-executable
    • bin: application/octet-stream
    • woff: font/woff
    • woff2: font/woff2
    • otf: font/otf
    • ttf: font/ttf
    • ai: application/postscript
    • swf: application/x-shockwave-flash
    • ogx: application/ogg

Class Methods Details

public function __construct(string$fNameOrAbsPath, string|null$fPath)Creates new instance of the class. This method will set the path and name to empty string. Also, it will set the size to 0 and ID to -1. Finally, it will set MIME type to "application/octet-stream"

Parameters:

  • string $fNameOrAbsPath The name of the file such as 'my-file.png'. This also can be the absolute path of the file (such as 'home/usr/ibrahim/my-file.png').
  • string|null $fPath The path of the file such as 'C:/Images/Test'. This can be null if absolute path of the file was provided for the first parameter.
public function __toString()Returns JSON string that represents basic file info. Returns: string public function append(string$data)Appends a string of data to the already existing data.

Parameters:

  • string $data A string that represents the extra data.
public function getAbsolutePath()Returns the full path to the file. The full path of the file is a string that contains the path of the file alongside its name. Assuming that the path is set to "C:/Users/Me/Documents" and file name is set to "my-doc.docx", This method will return something like "C:\Users\Me\Documents\my-do.docx".Returns: stringFull path to the file (e.g. 'root\images\hello.png'). If the name of the file is not set or the path is not set, the method will return empty string.public function getChunks(int$chunkSize, string$encodeOrDecode)Split file raw data into chunks of fixed size.

Parameters:

  • int $chunkSize The number of bytes in every chunk. If a negative number is given, default value is used which is 1000.
  • string $encodeOrDecode This parameter is used to base-64 decode or encode file data. The parameter can have one of 3 values:
    • e: Encode the raw data of the file.
    • d: Decode the raw data of the file.
    • none: Return the raw data of the file as it is. This is the default value.
    If any other value is given, the method will use 'e' since data is mostly will be moved ss chunks.
Returns: arrayThe method will return an array that holds file data as chunks.
public function getDir()Returns the directory at which the file exist on. The directory is simply the folder that contains the file. For example, the directory can be something like "C:\Users\Me\Documents". Note that the returned directory will be using backward slashes "\".Returns: stringThe directory at which the file exist on.public function getExtension()Extract file extension from file name and return it. File extension will depend on one of two things. If MIME of the file is set, then the extension of the file will depend on it. For example, if MIME of the file is 'video/mp4', the method will return the value 'mp4'. The other thing that will be checked is file name. If the extension is included in file name, it will be returned.Returns: stringA string such as 'mp3' or 'jpeg'. Default return value is 'bin' which stands for binary file.public function getFileMIMEType()Returns MIME type of the file. Note that if the file is specified by its path and name, the method File::read() must be called before calling this method to update its MIME type.Returns: stringMIME type of the file. If MIME type of the file is not set or not detected, the method will return 'application/octet-stream'.public function getID()Returns the ID of the file. This method is helpful in case the file is stored in database.Returns: stringThe ID of the file. If the ID is not set, the method will return -1.public function getLastModified(string$format)Returns the time at which the file was last modified. Note that this method will work only if the file exist in the file system.

Parameters:

  • string $format An optional format. The supported formats are the same formats which are supported by the function date().
Returns: string|intIf no format is provided, the method will return the time as integer. If a format is given, the method will return the time as specified by the format. If the file does not exist, the method will return 0.
public static function getMIMEType(string$ext)Returns MIME type of a file type. The method will try to find MIME type based on its extension. The method will look for MIME in the constant File::MIME_TYPES.

Parameters:

  • string $ext File extension without the suffix (such as 'jpg').
Returns: string|nullIf the extension MIME type is found, it will be returned. If not, the method will return null.
public function getName()Returns the name of the file. The name is used to construct the absolute path of the file in addition to its path.Returns: stringThe name of the file. If the name is not set, the method will return empty string.public function getPath()Returns the path of the file. The path is simply the folder that contains the file. For example, the path can be something like "C:\Users\Me\Documents". Note that the returned path will be using backward slashes "\".Returns: stringThe path to the file (such as "C:\Users\Me\Documents"). If the path is not set, the method will return empty string.public function getRawData(string$encodeOrDecode)Returns the raw data of the file. The raw data is simply a string. It can be binary string or any basic string.

Parameters:

  • string $encodeOrDecode This parameter is used to base-64 decode or encode file data. The parameter can have one of 3 values:
    • e: Encode the raw data of the file.
    • d: Decode the raw data of the file.
    • none: Return the raw data of the file as it is. This is the default value.
    If any other value is given, the method will use 'none'.
Returns: string|nullRaw data of the file. If no data is set, the method will return null.
public function getSize()Returns the size of the file in bytes. Note that if the file is specified by its path and name, the method File::read() must be called before calling this method to update its size.Returns: intSize of the file in bytes.public function isExist()Checks if the file exist or not. Returns: booleanIf the file exist, the method will return true. Other than that, the method will return false.public static function isFileExist(string$path)Checks if file exist or not without throwing errors. This method uses the function 'file_exists()' to check if a file is exist or not given its path. The only difference is that it will not throw an error if path is invalid.

Parameters:

public function read(int$from, int$to)Reads the file in binary mode. First of all, this method checks the existence of the file. If it is exist, it tries to open the file in binary mode 'rb'. If a resource is created, it is used to read the content of the file. Also, the method will try to set MIME type of the file. If MIME type was not detected, it will set to 'application/octet-stream'. If the method is unable to read the file, it will throw an exception.

Parameters:

  • int $from The byte at which the method will start reading from. If -1 is given, then the method will start reading from byte 0.
  • int $to The byte at which the method will read data to. If -1 is given, then the method will read till last byte. Default is -1.
public function remove()Removes a file given its name and path. Before calling this method, the name of the file and its path must be specified.Returns: booleanIf the file was removed, the method will return true. Other than that, the method will return false.public function setDir(string$dir)Sets the name of the directory at which the file exist on. The directory is simply the folder that contains the file. For example, the directory can be something like "C:/Users/Me/Documents". The directory can use forward slashes or backward slashes.

Parameters:

  • string $dir The directory which will contain the file. It must be non-empty string in order to set.
Returns: booleanThe method will return true if the directory is set. Other than that, the method will return false.
public function setId(string$id)Sets the ID of the file. This method is helpful in case the file is stored in database.

Parameters:

  • string $id The unique ID of the file.
public function setMIMEType(string$type)Sets the MIME type of the file. It is not recommended to update MIME type of the file manually. Only use this method for custom file types. MIME type will be set only if its non-empty string.

Parameters:

  • string $type MIME type (such as 'application/pdf')
public function setName(string$name)Sets the name of the file (such as 'my-image. png') The name is used to construct the absolute path of the file in addition to its path. The name of the file must include its extension (or suffix).

Parameters:

  • string $name The name of the file.
public function setPath(string$fPath)Sets the path of the file. The path is simply the folder that contains the file. For example, the path can be something like "C:/Users/Me/Documents". The path can use forward slashes or backward slashes.

Parameters:

  • string $fPath The folder which will contain the file. It must be non-empty string in order to set.
Returns: booleanThe method will return true if the path is set. Other than that, the method will return false.
public function setRawData(string$raw)Sets the binary representation of the file. The raw data is simply a string. It can be binary string or any basic string. Also, it can be a blob which was retrieved from a database.

Parameters:

  • string $raw Binary raw data of the file.
public function toJSON()Returns a JSON string that represents the file. Returns: JsonAn object of type 'Json' that contains file information. The object will have the following information:
{
  "id":"",
  "mime":"",
  "name":"",
  "path":"",
  "sizeInBytes":"",
  "sizeInKBytes":"",
  "sizeInMBytes":""
}
public function view(boolean$asAttachment)Display the file. If the raw data of the file is null, the method will try to read the file that was specified by the name and its path. If the method is unable to read the file, an exception is thrown.

Parameters:

  • boolean $asAttachment If this parameter is set to true, the header 'content-disposition' will have the attribute 'attachment' set instead of 'inline'. This will trigger 'save as' dialog to appear.
public function write(boolean$append, boolean$create)Write raw binary data into a file. The method will write the data using the binary write mode. If it fails, It will throw an exception.

Parameters:

  • boolean $append If the file already exist in the file system and this attribute is set to true, the new raw data will be appended to the file. Default is true.
  • boolean $create If the file does not exist and this attribute is set to true, the method will attempt to create the file. Default is false.
mdi-menuAll Classesmdi-chevron-leftAnchorBrCodeSnippetHTMLDocHTMLListHTMLNodeHTMLTableHeadNodeInputJsCodeLabelListItemOrderedListParagraphRadioGroupTableCellTableRowUnorderedListInvalidNodeNameExceptionTemplateNotFoundExceptionCaseConverterJsonJsonConverterJsonIJsonTypesPropertyAPIFilterAbstractWebServiceManagerInfoServiceParamTypesRequestRequestParameterResponseUriWebServicesManagerAccessAutoLoaderConfigConfigControllerDBEAbstractWebServiceExtendedWebServicesManagerFileLoggerPrivilegePrivilegesGroupThemeThemeLoaderUploadFileUploaderUserUtilWebFioriAppLanguageErrorBoxHTTPCodeViewMessageBoxServerErrViewStarterPageWebPageDatabaseSessionStorageDefaultSessionStorageMSSQLSessionDataTableMSSQLSessionsTableMySQLSessionDataTableMySQLSessionsTableSessionSessionOperationsSessionStorageSessionsManagerRouterRouterUriAbstractMiddlewareMiddlewareManagerEmailMessageSMTPAccountSMTPServerClassLoaderExceptionFileExceptionInitializationExceptionInvalidCRONExprExceptionMissingLangExceptionNoSuchThemeExceptionRoutingExceptionSMTPExceptionSessionExceptionUIExceptionAbstractJobCronCronEmailCronJobJobArgumentCronLoginViewCronTasksViewCronViewCronLoginServiceCronLogoutServiceCronServicesManagerForceCronExecutionServiceGetJobsServiceCLICLICommandInputStreamOutputStreamStdInStdOutClassWriterLangClassWriterQueryClassWriterServiceHolderThemeClassWriterWebServiceWriterCreateCLIClassHelperCreateCronJobCreateMiddlewareCreateTableCreateTableObjCreateThemeHelperCreateWebServiceAddCommandCreateCommandCronCommandHelpCommandListCronCommandListRoutesCommandListThemesCommandRunSQLQueryCommandSettingsCommandTestRouteCommandUpdateSettingsCommandUpdateTableCommandVersionCommandAbstractQueryColumnColumnFactoryConditionConnectionConnectionInfoDatabaseDatabaseExceptionDateTimeValidatorEntityMapperExpressionForeignKeyJoinTableResultSetSelectExpressionTableWhereExpressionMySQLColumnMySQLConnectionMySQLQueryMySQLTableMSSQLColumnMSSQLConnectionMSSQLQueryMSSQLTableAbstractCollectionComparableLinkedListNodeQueueStack
Powered By: WebFiori Framework, Vue and VuetifyAll Rights Reserved © 2018 - 2024