Temp
Temp
Create a temporary file.
This class will create and manage a temporary file that is stored in the application runtime directory in a subdirectory called 'temp'. The file is not stateful, meaning it can only be used for the lifetime of the execution and once the file object falls out of scope it will be deleted automatically.
- Full name:
\Hazaar\File\Temp
- Parent class:
\Hazaar\File
Methods
__construct
Temporary file constructor.
public __construct(mixed $name = null): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$name | mixed | An option name to give the temp file. Leaving this null will auto-generate a name. |
__destruct
Temporary file destructor.
public __destruct(): mixed
This method will clean up the file on disk when the object falls out of scope.
Inherited methods
__construct
public __construct(mixed $file = null, ?\Hazaar\File\Manager $manager = null, ?string $relative_path = null): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$file | mixed | |
$manager | ?\Hazaar\File\Manager | |
$relative_path | ?string |
__toString
public __toString(): string
backend
public backend(): string
getManager
public getManager(): \Hazaar\File\Manager
get
public static get(string $url, ?\Hazaar\HTTP\Client $client = null): bool|\Hazaar\File
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$url | string | |
$client | ?\Hazaar\HTTP\Client |
registerFilter
Content filters.
public registerFilter(int $type, \Closure|string $callable): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$type | int | |
$callable | \Closure|string |
setMeta
public setMeta(array<string,mixed> $values): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$values | array<string,mixed> |
getMeta
public getMeta(?string $key = null): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | ?string |
toString
public toString(): string
basename
public basename(string $suffix = ''): string
Parameters:
Parameter | Type | Description |
---|---|---|
$suffix | string |
dirname
public dirname(): string
name
public name(): string
fullpath
public fullpath(): string
relativepath
Get the relative path of the file.
public relativepath(mixed $path = null): bool|string
If the file was returned from a [[Hazaar\File\Dir]] object, then it will have a stored relative path. Otherwise any file/path can be provided in the form of another [[Hazaar\File\ object, [[Hazaar\File\Dir]] object, or string path, and the relative path to the file will be returned.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | mixed | optional path to use as the relative path |
Return Value:
The relative path. False when $path is not valid
setRelativePath
public setRelativePath(string $path): void
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string |
extension
public extension(): string
size
public size(): ?int
exists
public exists(): bool
realpath
public realpath(): string
isReadable
public isReadable(): bool
isWritable
public isWritable(): bool
isFile
public isFile(): bool
isDir
public isDir(): bool
isLink
public isLink(): bool
dir
public dir(): ?\Hazaar\File\Dir
parent
public parent(): \Hazaar\File\Dir
type
public type(): ?string
ctime
public ctime(): ?int
mtime
public mtime(): ?int
touch
public touch(): bool
atime
public atime(): ?int
hasContents
public hasContents(): bool
getContents
Returns the current contents of the file.
public getContents(int $offset = -1, ?int $maxlen = null): string
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | int | |
$maxlen | ?int |
putContents
Put contents directly writes data to the storage backend without storing it in the file object itself.
public putContents(string $data, bool $overwrite = true): ?int
NOTE: This function is called internally to save data that has been updated in the file object.
Parameters:
Parameter | Type | Description |
---|---|---|
$data | string | The data to write |
$overwrite | bool | Overwrite data if it exists |
setContents
Sets the current contents of the file in memory.
public setContents(string $bytes): ?int
Calling this function does not directly update the content of the file "on disk". To do that you must call the File::save() method which will commit the data to storage.
Parameters:
Parameter | Type | Description |
---|---|---|
$bytes | string | The data to set as the content |
setDecodedContents
Set the contents from an encoded string.
public setDecodedContents(string $bytes): bool
Currently this supports only data URI encoded strings. I have made this generic in case I come across other types of encodings that will work with this method.
Parameters:
Parameter | Type | Description |
---|---|---|
$bytes | string |
getEncodedContents
Return the contents of the file as a data URI encoded string.
public getEncodedContents(): string
This function is basically the opposite of Hazaar\File::set_decoded_contents() and will generate a data URI based on the current MIME content type and the contents of the file.
save
Saves the current in-memory content to the storage backend.
public save(): ?int
Internally this calls File::putContents() to write the data to the backend.
saveAs
Saves this file objects content to another file name.
public saveAs(string $filename, bool $overwrite = false): ?int
Parameters:
Parameter | Type | Description |
---|---|---|
$filename | string | The filename to save as |
$overwrite | bool | Boolean flag to indicate that the destination should be overwritten if it exists |
unlink
Deletes the file from storage.
public unlink(): bool
md5
Generate an MD5 checksum of the current file content.
public md5(): string
base64
Return the base64 encoded content.
public base64(): string
parseJSON
Returns the contents as decoded JSON.
public parseJSON(bool $assoc = false): array|\stdClass
If the content is a JSON encoded string, this will decode the string and return it as a stdClass object, or an associative array if the $assoc parameter is TRUE.
If the content can not be decoded because it is not a valid JSON string, this method will return FALSE.
Parameters:
Parameter | Type | Description |
---|---|---|
$assoc | bool | Return as an associative array. Default is to use stdClass. |
moveTo
public moveTo(string $destination, bool $overwrite = false, bool $create_dest = false, ?\Hazaar\File\Manager $dstManager = null): bool|\Hazaar\File
Parameters:
Parameter | Type | Description |
---|---|---|
$destination | string | |
$overwrite | bool | |
$create_dest | bool | |
$dstManager | ?\Hazaar\File\Manager |
copyTo
Copy the file to another folder.
public copyTo(string $destination, bool $overwrite = false, bool $create_dest = false, \Hazaar\File\Manager $dstManager = null): bool|\Hazaar\File
This differs to copy() which expects the target to be the full new file pathname.
Parameters:
Parameter | Type | Description |
---|---|---|
$destination | string | The destination folder to copy the file into |
$overwrite | bool | overwrite the destination file if it exists |
$create_dest | bool | Flag that indicates if the destination folder should be created. If the destination does not exist an error will be thrown. |
$dstManager | \Hazaar\File\Manager | The destination file manager. Defaults to the same manager as the source. |
Throws:
copy
Copy the file to another folder and filename.
public copy(string $destination, bool $overwrite = false, bool $create_dest = false, mixed $dstManager = null): bool|\Hazaar\File
This differs from copyTo which expects the target to be a folder
Parameters:
Parameter | Type | Description |
---|---|---|
$destination | string | The destination folder and file name to copy the file into |
$overwrite | bool | overwrite the destination file if it exists |
$create_dest | bool | Flag that indicates if the destination folder should be created. If the destination does not exist an error will be thrown. |
$dstManager | mixed | The destination file manager. Defaults to the same manager as the source. |
Throws:
mimeContentType
public mimeContentType(): ?string
setMimeContentType
public setMimeContentType(string $type): void
Parameters:
Parameter | Type | Description |
---|---|---|
$type | string |
thumbnailURL
public thumbnailURL(int $width = 100, int $height = 100, string $format = 'jpeg', array<string,int|string> $params = []): ?string
Parameters:
Parameter | Type | Description |
---|---|---|
$width | int | |
$height | int | |
$format | string | |
$params | array<string,int|string> |
previewURL
public previewURL(array<string,string> $params = []): ?string
Parameters:
Parameter | Type | Description |
---|---|---|
$params | array<string,string> |
directURL
public directURL(): ?string
mediaURL
public mediaURL(null|string|\Hazaar\HTTP\URL $set_path = null): \Hazaar\HTTP\URL
Parameters:
Parameter | Type | Description |
---|---|---|
$set_path | null|string|\Hazaar\HTTP\URL |
toArray
public toArray(string $delimiter = "
"): string[]
Parameters:
Parameter | Type | Description |
---|---|---|
$delimiter | string |
readCSV
Return the CSV content as a parsed array.
public readCSV(bool $use_header_row = false): string[][]
Parameters:
Parameter | Type | Description |
---|---|---|
$use_header_row | bool | Indicates if a header row should be parsed and used to build an associative array. In this case the keys in the returned array will be the values from the first row, which is normally a header row. |
getcsv
Returns a line from the file pointer and parse for CSV fields.
public getcsv(int $length, string $delimiter = ',', string $enclosure = '"', string $escape = '\'): string[]
Parameters:
Parameter | Type | Description |
---|---|---|
$length | int | Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). Otherwise the line is split in chunks of length characters, unless the split would occur inside an enclosure. Omitting this parameter (or setting it to 0 in PHP 5.1.0 and later) the maximum line length is not limited, which is slightly slower. |
$delimiter | string | the optional delimiter parameter sets the field delimiter (one character only) |
$enclosure | string | the optional enclosure parameter sets the field enclosure character (one character only) |
$escape | string | the optional escape parameter sets the escape character (one character only) |
putcsv
Writes an array to the file in CSV format.
public putcsv((int|string)[] $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\'): null|int
Parameters:
Parameter | Type | Description |
---|---|---|
$fields | (int|string)[] | Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). Otherwise the line is split in chunks of length characters, unless the split would occur inside an enclosure. Omitting this parameter (or setting it to 0 in PHP 5.1.0 and later) the maximum line length is not limited, which is slightly slower. |
$delimiter | string | the optional delimiter parameter sets the field delimiter (one character only) |
$enclosure | string | the optional enclosure parameter sets the field enclosure character (one character only) |
$escape | string | the optional escape parameter sets the escape character (one character only) |
rename
Renames a file or directory.
public rename(string $newname): bool
NOTE: This will not work if the file is currently opened by another process.
Parameters:
Parameter | Type | Description |
---|---|---|
$newname | string | The new name. Must not be an absolute/relative path. If you want to move the file use File::moveTo(). |
delete
public static delete(string $path): bool
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string |
jsonSerialize
public jsonSerialize(): mixed
perms
public perms(): ?int
chmod
public chmod(int $mode): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$mode | int |
isEncrypted
Check if a file is encrypted using the built-in Hazaar encryption method.
public isEncrypted(): bool
encrypt
public encrypt(): bool
decrypt
public decrypt(): bool
isOpen
public isOpen(): bool
open
public open(string $mode): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$mode | string |
read
public read(int $length): false|string
Parameters:
Parameter | Type | Description |
---|---|---|
$length | int |
write
public write(string $bytes, int $length): false|int
Parameters:
Parameter | Type | Description |
---|---|---|
$bytes | string | |
$length | int |
seek
public seek(int $offset, int $whence = SEEK_SET): int
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | int | |
$whence | int |
tell
public tell(): false|int
eof
public eof(): bool
truncate
public truncate(int $size): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$size | int |
lock
public lock(int $operation, ?int& $wouldblock = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$operation | int | |
$wouldblock | ?int |
flush
public flush(): bool
gets
public gets(?int $length = null): false|string
Parameters:
Parameter | Type | Description |
---|---|---|
$length | ?int |
close
public close(): bool
filterIn
Internal content filter.
protected filterIn(string& $content): bool
Checks if the content is modified in some way using a BOM mask. Currently this is used to determine if a file is encrypted and automatically decrypts it if an encryption key is available.
Parameters:
Parameter | Type | Description |
---|---|---|
$content | string |
filterOut
Internal content filter.
protected filterOut(string& $content): bool
Checks if the content is modified in some way using a BOM mask. Currently this is used to determine if a file is encrypted and automatically decrypts it if an encryption key is available.
Parameters:
Parameter | Type | Description |
---|---|---|
$content | string |
getEncryptionKey
private getEncryptionKey(): string
Automatically generated on 2024-11-14