Jet\IO_File

An important difference from the normal use of PHP functions is that when an operation fails, the Jet\IO_File_Exception exception is always thrown.

Uses the Jet\SysConf_Jet_IO::setFileMod() and Jet\SysConf_Jet_IO::setDirMod() settings for default file and directory permissions.

Overview of methods

Method Meaning of
public static exists(
string $file_path
): bool
Indicates whether the file exists (and it is a file, not a directory).
public static isWritable(
string $file_path
): bool
Indicates whether the file is writable.
public static isReadable(
string $file_path
): bool
Indicates whether the file is readable.
public static getSize(
string $file_path
): int
Returns the file size.

In case of failure, throws an exception.
public static getMimeType(
string $file_path,
bool $without_charset = true
): string
Returns the MIME type of the file.

By default, PHP uses the finfo

However, it is also possible to use Jet\SysConf_Jet_IO::setExtensionsMimesMap() to set a map of mime types linked to file extensions.
public static write(
string $file_path,
string $data
): void
Writes the data to a file.

If the file existed, it is overwritten.

If there is no directory to write the file to, it is created.

In case of failure, it throws an exception.
public static append(
string $file_path,
mixed $data
): void
Writes data to the file.

If the file exists, then the data is added to the end of the file, otherwise the file is created.

If there is no directory to write the file to, it is created.

In case of failure, it throws an exception.
public static chmod(
string $file_path,
?int $chmod_mask = null
): void
Sets file rights.

If the permission is not directly specified, then the default value is used (Jet\SysConf_Jet_IO::setFileMod())

In case of failure, it throws an exception.
public static read(
string $file_path
): string
Read the contents of the file.

It can read data from HTTP(s) sources - which is a given. But at the same time, the method can handle the situation where the HTTP response is compressed (gzipped).

In case of failure, it throws an exception.
public static rename(
string $source_path,
string $target_path,
bool $overwrite_if_exists = true
): void
Renames/moves the file.

If $overwrite_if_exists = true and the file already exists at the target location, then it is first deleted and then replaced by the source file.

If the $overwrite_if_exists = false and the file already exists at the target location, then an exception is thrown.

In case of failure, it throws an exception.
public static move(
string $source_path,
string $target_path,
bool $overwrite_if_exists = true
): void
Alias of rename method.
public static copy(
string $source_path,
string $target_path,
bool $overwrite_if_exists = true
): void
Copies a file.

If the $overwrite_if_exists = true and the file already exists at the target location, then it is first deleted and then replaced by the source file.

If $overwrite_if_exists = false and the file already exists at the destination, then an exception is thrown.

In case of failure, it throws an exception.
public static delete(
string $file_path
): void
Deletes the file.

In case of failure, it throws an exception.
public static moveUploadedFile(
string $source_path,
string $target_path,
bool $overwrite_if_exists = true
): void
Moves the uploaded file from the temporary location to the destination location. This verifies that it is indeed the uploaded file.

In case of failure, it throws an exception.
public static getMaxUploadSize(
): int
Returns the maximum possible size of uploaded files in bytes.

Considers both upload_max_filesize and post_max_size.
public static getMaxFileUploads(
): int
Returns the theoretical maximum number of files to be recorded in parallel.
public static send(
string $file_path,
?string $file_name = null,
?string $file_mime = null,
?int $file_size = null,
bool $force_download = false
): void
Sends the file for download. In doing so, it sets HTTP headers. The only mandatory parameter is the full path to the file and everything else (type, size, name) is found by the method.

The $force_download parameter sends HTTP headers in the form that will make the client browser offer the file directly for saving.
public static getHttpResponseHeader(
): array
If you use the IO_File::read() method to download a file from an HTTP source, then this method provides the http headers of the response.
public static writeDataAsPhp(
string $path,
array $data,
bool $reset_cache=true
) : void
Writes the data in a like PHP file - used for internal purposes.
Previous chapter
I/O
Next chapter
Jet\IO_Dir