Kotchasan

UploadedFile
in package
implements UploadedFileInterface

Class for handling uploaded files.

Tags
see
https://www.kotchasan.com/

Interfaces, Classes and Traits

UploadedFileInterface
Value object representing a file uploaded through an HTTP request.

Table of Contents

$error  : int
The upload error code (UPLOAD_ERR_XXX).
$ext  : string|null
The file extension of the uploaded file.
$isMoved  : bool
Indicates whether the file has been moved.
$mime  : string|null
The MIME type of the file.
$name  : string|null
The name of the uploaded file.
$sapi  : bool
Indicates if the upload is from a SAPI environment.
$size  : int|null
The size of the uploaded file.
$stream  : Stream|null
The file stream.
$tmp_name  : string|null
The path to the uploaded file.
__construct()  : mixed
Creates a new UploadedFile instance.
copyTo()  : bool
Copies the uploaded file to a new location.
cropImage()  : bool|string
ฟังก์ชั่น ตัดรูปภาพ ตามขนาดที่กำหนด และย้ายไปยังปลายทาง รูปภาพปลายทางจะมีขนาดเท่าที่กำหนด หากรูปภาพต้นฉบับมีขนาดหรืออัตราส่วนไม่พอดีกับขนาดของภาพปลายทาง รูปภาพจะถูกตัดขอบออกหรือจะถูกขยาย เพื่อให้พอดีกับรูปภาพปลายทางที่ต้องการ ผลลัพท์จะได้ไฟล์รูปภาพ jpg เท่านั้น สำเร็จคืนค่า true ไม่สำเร็จคืนค่าข้อความผิดพลาด
getCleanFilename()  : string
อ่านชื่อไฟล์จากไฟล์ที่อัปโหลดและตัดตัวอักษที่ไม่สามารถใช้เป็นชื่อไฟล์ได้ออก ยอมรับ ภาษาอังกฤษ ตัวเลข ( ) _ - และ .(จุด) เท่านั้น นอกเหนือจากนั้นจะถูกแทนที่ด้วย $replace ติดกันไม่เกิน 1 ตัวอักษร
getClientFileExt()  : string
คืนค่านามสกุลของไฟล์อัปโหลด ตัวพิมพ์เล็ก เช่น jpg
getClientFilename()  : string|null
อ่านชื่อไฟล์ (ต้นฉบับ) ของไฟล์ที่อัปโหลด
getClientMediaType()  : string|null
อ่าน MIME Type ของไฟล์
getError()  : int
อ่านข้อผิดพลาดของไฟล์อัปโหลด คืนค่า UPLOAD_ERR_XXX
getErrorMessage()  : string
อ่านข้อผิดพลาดของไฟล์อัปโหลด เป็นข้อความ
getSize()  : int|null
Retrieves the size of the uploaded file.
getStream()  : StreamInterface
Retrieves the uploaded file as a stream.
getTempFileName()  : string|null
Retrieves the path of the uploaded file, including the temporary directory.
getUploadSize()  : string|int
Retrieves the configured maximum upload file size.
hasError()  : bool
Checks if an error occurred during the upload process.
hasUploadFile()  : bool
Checks if an upload file exists.
moveTo()  : bool
Moves the uploaded file to the specified target path.
resizeImage()  : string
Resizes an image.
validFileExt()  : bool
Checks if the file extension is valid.
check()  : bool
Checks if the uploaded file is valid and the target directory is writable.

Properties

$error

The upload error code (UPLOAD_ERR_XXX).

private int $error

$ext

The file extension of the uploaded file.

private string|null $ext

$isMoved

Indicates whether the file has been moved.

private bool $isMoved = false

$mime

The MIME type of the file.

private string|null $mime

$name

The name of the uploaded file.

private string|null $name

$sapi

Indicates if the upload is from a SAPI environment.

private bool $sapi = false

$size

The size of the uploaded file.

private int|null $size

$tmp_name

The path to the uploaded file.

private string|null $tmp_name

Methods

__construct()

Creates a new UploadedFile instance.

public __construct(string $path, string $originalName[, string|null $mimeType = null ][, int|null $size = null ][, int|null $error = null ][, bool $sapi = true ]) : mixed
Parameters
$path : string

The path to the uploaded file.

$originalName : string

The original name of the uploaded file.

$mimeType : string|null = null

The MIME type of the uploaded file.

$size : int|null = null

The size of the uploaded file.

$error : int|null = null

The upload error code (UPLOAD_ERR_XXX).

$sapi : bool = true

Indicates if the upload is in a SAPI environment.

Return values
mixed

copyTo()

Copies the uploaded file to a new location.

public copyTo(string $targetPath) : bool
Parameters
$targetPath : string

The destination path to which the file should be moved.

Tags
throws
RuntimeException

If an error occurs while copying the file.

throws
InvalidArgumentException

If the target directory is not writable.

Return values
bool

True on success, false otherwise.

cropImage()

ฟังก์ชั่น ตัดรูปภาพ ตามขนาดที่กำหนด และย้ายไปยังปลายทาง รูปภาพปลายทางจะมีขนาดเท่าที่กำหนด หากรูปภาพต้นฉบับมีขนาดหรืออัตราส่วนไม่พอดีกับขนาดของภาพปลายทาง รูปภาพจะถูกตัดขอบออกหรือจะถูกขยาย เพื่อให้พอดีกับรูปภาพปลายทางที่ต้องการ ผลลัพท์จะได้ไฟล์รูปภาพ jpg เท่านั้น สำเร็จคืนค่า true ไม่สำเร็จคืนค่าข้อความผิดพลาด

public cropImage(array<string|int, mixed> $exts, string $targetPath, int $width, int $height[, string $watermark = '' ]) : bool|string
Parameters
$exts : array<string|int, mixed>

นามสกุลของไฟล์รูปภาพที่ยอมรับ เช่น [jpg, gif, png]

$targetPath : string

path และชื่อไฟล์ของไฟล์รูปภาพปลายทาง

$width : int

ความกว้างของรูปภาพที่ต้องการ

$height : int

ความสูงของรูปภาพที่ต้องการ

$watermark : string = ''

(optional) ข้อความลายน้ำ

Tags
throws
InvalidArgumentException

ข้อผิดพลาดหากที่อยู่ปลายทางไม่สามารถเขียนได้

throws
RuntimeException

ข้อผิดพลาดไม่สามารถสร้างรูปภาพได้

Return values
bool|string

getCleanFilename()

อ่านชื่อไฟล์จากไฟล์ที่อัปโหลดและตัดตัวอักษที่ไม่สามารถใช้เป็นชื่อไฟล์ได้ออก ยอมรับ ภาษาอังกฤษ ตัวเลข ( ) _ - และ .(จุด) เท่านั้น นอกเหนือจากนั้นจะถูกแทนที่ด้วย $replace ติดกันไม่เกิน 1 ตัวอักษร

public getCleanFilename([string $replace = '_' ]) : string
Parameters
$replace : string = '_'

ตัวอักษรที่จะแทนที่อักขระไที่ไม่ต้องการ ถ้าไม่ระบุจะใช้ _ (ขีดล่าง)

Return values
string

getClientFileExt()

คืนค่านามสกุลของไฟล์อัปโหลด ตัวพิมพ์เล็ก เช่น jpg

public getClientFileExt() : string
Return values
string

getClientFilename()

อ่านชื่อไฟล์ (ต้นฉบับ) ของไฟล์ที่อัปโหลด

public getClientFilename() : string|null
Return values
string|null

getClientMediaType()

อ่าน MIME Type ของไฟล์

public getClientMediaType() : string|null
Return values
string|null

getError()

อ่านข้อผิดพลาดของไฟล์อัปโหลด คืนค่า UPLOAD_ERR_XXX

public getError() : int
Return values
int

getErrorMessage()

อ่านข้อผิดพลาดของไฟล์อัปโหลด เป็นข้อความ

public getErrorMessage() : string
Tags
staticvar

array $errors

Return values
string

getSize()

Retrieves the size of the uploaded file.

public getSize() : int|null
Return values
int|null

The size of the uploaded file in bytes, or null if the size is unknown.

getStream()

Retrieves the uploaded file as a stream.

public getStream() : StreamInterface
Tags
throws
RuntimeException

If the file has already been moved or if the file is not found.

Return values
StreamInterface

The uploaded file stream.

getTempFileName()

Retrieves the path of the uploaded file, including the temporary directory.

public getTempFileName() : string|null
Return values
string|null

The path of the uploaded file, or null if the file is not available.

getUploadSize()

Retrieves the configured maximum upload file size.

public static getUploadSize([bool $return_byte = false ]) : string|int
Parameters
$return_byte : bool = false

False (default) to return the size as a string (e.g., '2M'), true to return the size in bytes.

Return values
string|int

The maximum upload file size as a string or an integer (bytes).

hasError()

Checks if an error occurred during the upload process.

public hasError() : bool
Return values
bool

True if an error occurred, false otherwise.

hasUploadFile()

Checks if an upload file exists.

public hasUploadFile() : bool
Return values
bool

True if an upload file exists, false otherwise.

moveTo()

Moves the uploaded file to the specified target path.

public moveTo(string $targetPath) : bool
Parameters
$targetPath : string

The path where the file will be moved.

Tags
throws
RuntimeException

If the file has already been moved or an error occurred during the move operation.

throws
InvalidArgumentException

If the target directory is not writable.

Return values
bool

True if the file was successfully moved, false otherwise.

resizeImage()

Resizes an image.

public resizeImage(array<string|int, mixed> $exts, string $target, string $name, int $width[, string $watermark = '' ]) : string
Parameters
$exts : array<string|int, mixed>

An array of valid file extensions.

$target : string

The target directory for saving the resized image.

$name : string

The name of the resized image.

$width : int

The desired width of the resized image.

$watermark : string = ''

Optional watermark to be applied on the resized image.

Tags
throws
RuntimeException

If unable to create the image.

throws
InvalidArgumentException

If the file extension or target directory is invalid.

Return values
string

The path to the resized image.

validFileExt()

Checks if the file extension is valid.

public validFileExt(array<string|int, mixed> $exts) : bool
Parameters
$exts : array<string|int, mixed>

An array of valid file extensions.

Return values
bool

True if the file extension is valid, false otherwise.

check()

Checks if the uploaded file is valid and the target directory is writable.

private check(array<string|int, mixed> $exts, string $targetDir) : bool
Parameters
$exts : array<string|int, mixed>

An array of valid file extensions.

$targetDir : string

The target directory.

Tags
throws
RuntimeException

If the type of file is invalid.

throws
InvalidArgumentException

If the target directory is not writable.

Return values
bool

True if the file is valid and the directory is writable, false otherwise.

Search results