Upload
in package
PHP Library to help you build your own file sharing website.
Tags
Table of Contents
- $controller : string
- Class Controller Path
- $file_id : string
- File ID for the database
- $files : array<string|int, mixed>
- Array for the all uploaded files informations
- $filter_array : array<string|int, mixed>
- Array For File Protection Filter
- $hash_id : int
- Image minimum width
- $hash_name : int
- Image minimum width
- $logs : array<string|int, mixed>
- System Logs Array
- $max_height : int
- Image miximum height
- $max_width : int
- Image miximum width
- $message : array<string|int, mixed>
- Array list of error codes and the messages
- $min_height : int
- Image minimum height
- $min_width : int
- Image minimum width
- $name_array : array<string|int, mixed>
- Array For Filename Protection Filter
- $site_url : string
- Website URL to use with Download URL
- $size : int
- Size limit for protection
- $upload_folder : array<string|int, mixed>
- Array For the upload folder data
- $upload_input : array<string|int, mixed>
- HTML File Input
- $user_id : string
- User ID for the database
- __construct() : void
- Class Constructor to initialize attributes
- addFile() : void
- Function to add a file to the files array
- addLog() : void
- Add a message the system log
- callback() : mixed
- Create a callback function when needed after or before an operation
- checkDimenstion() : bool
- Extra Firewall 1: Check an image dimenstions aginst the class dimenstions
- checkExtension() : bool
- Firewall 1: Check File Extension
- checkForbidden() : bool
- Firewall 3: Check File Name is Forbidden
- checkIfNotEmpty() : bool
- Function to check if the HTML input is empty or not
- checkMime() : bool
- Firewall 2: Check File MIME Type
- checkSize() : bool
- Firewall 4: Check file size limit
- createUploadFolder() : void
- Function to create an upload folder and secure it
- createUserCloud() : true
- Create a folder for a spesfic user
- enableProtection() : void
- Enable File Uploading Protection and Filters
- fixArray() : array<string|int, mixed>
- Fix file input array to make it easy to iterate through it
- fixintOverflow() : float
- Ensure correct value for big ints
- formatBytes() : string
- Function that format file bytes to a readable format
- generateDeleteLink() : string
- Generate a delete link
- generateDirectDownloadLink() : string
- Generate a direct download link
- generateDownloadLink() : string
- Generate a download link
- generateFileID() : string
- Generate a Uniqe ID for each uploaded file
- generateQrCode() : string
- Generate a Qr Code of the download url
- generateUserID() : string
- Generate a User ID for each uploaded file
- getDate() : int|bool
- Get the date of the uploaded file
- getExtension() : string
- Function to return the file input extension
- getFileID() : string
- Get the unique file id
- getFiles() : array<string|int, mixed>
- Function to return an array with all the uploaded files information
- getFileType() : string
- Function that return the uploaded file MIME type
- getJSON() : string
- Get all the uploaded file information in JSON
- getLog() : int
- Get a system log message by an array index id
- getLogs() : array<string|int, mixed>
- Get all logs from system log to view them
- getMessage() : string
- Function to get a log message using a message index id
- getName() : string
- Return the name of the uploaded file
- getSize() : float
- Return the size of the uploaded file as bytes
- getTempName() : string
- Return the PHP Generated name for the uploaded file
- getUploadDirFiles() : array<string|int, mixed>|bool
- Return the files from the upload folder to view them
- getUserCloud() : mixed
- getUserID() : string
- Get the unique user id for security
- hashName() : true
- Return an "SHA1 Hashed File Name" of the uploaded file
- isDir() : bool
- Check if a directory exist and it is a real directory
- isFile() : bool
- Check if a file exist and it is a real file
- isImage() : bool
- Extra Firewall 2: Function to check if uploaded file is an image
- moveFile() : bool
- Function to upload file to the server using chunck system
- protectFoler() : void
- Function to potect a folder
- sanitize() : string
- Function that helps with input filter and sanitize
- setController() : void
- Function to set the controller when needed
- setFileID() : void
- Set the unique file id manualy when needed
- setForbiddenFilter() : void
- Set Forbidden array to a custom list when needed
- setINI() : void
- Set php.ini settings using an array
- setMaxDimenstion() : void
- Function to set the miximum class image dimenstions to validate them
- setMinDimenstion() : void
- Function to set the minimum class image dimenstions to validate them
- setProtectionFilter() : void
- Set Extension array to a custom list when needed
- setSiteUrl() : void
- Set the site url manualy when needed to generate links
- setSizeLimit() : void
- Set file size limit when needed
- setUpload() : void
- Function to set upload input when needed
- setUploadController() : void
- Set the upload controller file
- setUploadFolder() : void
- Set upload folder when needed
- setUserID() : void
- Set the unique user id manualy when needed
- sizeInBytes() : float
- Return any type of readable storage size to bytes
- upload() : bool
- Function to upload the file to the server
- getMime() : string
- Function to get the MIME type using the server
Properties
$controller
Class Controller Path
private
string
$controller
$file_id
File ID for the database
private
string
$file_id
$files
Array for the all uploaded files informations
private
array<string|int, mixed>
$files
= []
$filter_array
Array For File Protection Filter
private
array<string|int, mixed>
$filter_array
= []
$hash_id
Image minimum width
private
int
$hash_id
$hash_name
Image minimum width
private
int
$hash_name
$logs
System Logs Array
private
array<string|int, mixed>
$logs
= []
$max_height
Image miximum height
private
int
$max_height
$max_width
Image miximum width
private
int
$max_width
$message
Array list of error codes and the messages
private
array<string|int, mixed>
$message
= [0 => "File has been uploaded.", 1 => "Invalid file format.", 2 => "Failed to get MIME type.", 3 => "File is forbidden.", 4 => "Exceeded filesize limit.", 5 => "Please select a file", 6 => "File already exist.", 7 => "Failed to move uploaded file.", 8 => "The uploaded file's height is too large.", 9 => "The uploaded file's width is too large.", 10 => "The uploaded file's height is too small.", 11 => "The uploaded file's width is too small.", 12 => "The uploaded file's is too small.", 13 => "The uploaded file is not a valid image.", 14 => "Opreation does not exist."]
$min_height
Image minimum height
private
int
$min_height
$min_width
Image minimum width
private
int
$min_width
$name_array
Array For Filename Protection Filter
private
array<string|int, mixed>
$name_array
= []
$site_url
Website URL to use with Download URL
private
string
$site_url
$size
Size limit for protection
private
int
$size
$upload_folder
Array For the upload folder data
private
array<string|int, mixed>
$upload_folder
= []
Example: ["folder_name" => "upload", "folder_path" => "upload/"]
$upload_input
HTML File Input
private
array<string|int, mixed>
$upload_input
Example: $_FILES['file']
$user_id
User ID for the database
private
string
$user_id
Methods
__construct()
Class Constructor to initialize attributes
public
__construct([array<string|int, mixed> $upload_input = null ][, array<string|int, mixed> $upload_folder = [] ][, string $controller = null ][, mixed $site_url = '' ][, string $upload_controller = "upload.php" ][, int $size = "5 GB" ][, int $max_height = null ][, int $max_width = null ][, int $min_height = null ][, int $min_width = null ][, string $file_id = null ][, string $user_id = null ]) : void
Parameters
- $upload_input : array<string|int, mixed> = null
-
An array of the upload file information coming from $_FILES
- $upload_folder : array<string|int, mixed> = []
-
An array that contain the upload folder full path and name
- $controller : string = null
-
The folder name of folder that contains the json filters and the class file
- $site_url : mixed = ''
- $upload_controller : string = "upload.php"
-
The upload controller that contains the factory code like upload.php
- $size : int = "5 GB"
-
The miximum size that the class allow to upload
- $max_height : int = null
-
The miximum image height allowed
- $max_width : int = null
-
The miximum image width allowed
- $min_height : int = null
-
The minimum image height allowed
- $min_width : int = null
-
The minimum image width allowed
- $file_id : string = null
-
A uniqge if for the file to validate that the file exist
- $user_id : string = null
-
A uniqge if for the user to validate the file owner
Return values
void —addFile()
Function to add a file to the files array
public
addFile(string $json_string) : void
Parameters
- $json_string : string
-
The JSON string the contains the file information
Return values
void —addLog()
Add a message the system log
public
addLog(mixed $message[, mixed $id = null ]) : void
Parameters
- $message : mixed
-
The message id from the messsages array or as raw string
- $id : mixed = null
-
The array index that you want to assign the message too
Return values
void —callback()
Create a callback function when needed after or before an operation
public
callback(callable $function[, mixed $args = [] ]) : mixed
Parameters
- $function : callable
-
A callback function to execute
- $args : mixed = []
-
A single paramter or an array that contains multiple paramters
Return values
mixed —Return the callback function output
checkDimenstion()
Extra Firewall 1: Check an image dimenstions aginst the class dimenstions
public
checkDimenstion([int $opreation = 2 ]) : bool
Parameters
- $opreation : int = 2
-
Filters opreations from 0 to 5
Return values
bool —Return true if an image size passed this filter otherwise false
checkExtension()
Firewall 1: Check File Extension
public
checkExtension() : bool
Return values
bool —Return true it the uploaded file extenstion is allowed
checkForbidden()
Firewall 3: Check File Name is Forbidden
public
checkForbidden() : bool
Return values
bool —Return true if the name is forbidden
checkIfNotEmpty()
Function to check if the HTML input is empty or not
public
checkIfNotEmpty() : bool
Return values
bool —Return true if the the input contain a file false otherwise
checkMime()
Firewall 2: Check File MIME Type
public
checkMime() : bool
Return values
bool —Return true if the uploaded file MIME type is allowed
checkSize()
Firewall 4: Check file size limit
public
checkSize() : bool
Return values
bool —Return true if the uploaded file size does not exceed the limit
createUploadFolder()
Function to create an upload folder and secure it
public
createUploadFolder(string $folder_name) : void
Parameters
- $folder_name : string
-
The folder name you want to create as an upload folder
Return values
void —createUserCloud()
Create a folder for a spesfic user
public
createUserCloud(mixed $main_upload_folder) : true
Parameters
- $main_upload_folder : mixed
Return values
true —Returns true if the folder is created
enableProtection()
Enable File Uploading Protection and Filters
public
enableProtection() : void
Return values
void —fixArray()
Fix file input array to make it easy to iterate through it
public
fixArray(array<string|int, mixed> $file_post) : array<string|int, mixed>
Parameters
- $file_post : array<string|int, mixed>
-
The unarranged files array to fix
Return values
array<string|int, mixed> —Return a fixed and arranged files array based on PHP standerds
fixintOverflow()
Ensure correct value for big ints
public
fixintOverflow(int $int) : float
Parameters
- $int : int
Return values
float —formatBytes()
Function that format file bytes to a readable format
public
formatBytes(int $bytes[, int $precision = 2 ]) : string
Example: 7201450 to 7.2 MB
Parameters
- $bytes : int
-
The file size that you want to convert in bytes
- $precision : int = 2
-
The precision of the convertion how many digits you want after the dot
Return values
string —Return the bytes as readable format
generateDeleteLink()
Generate a delete link
public
generateDeleteLink() : string
Return values
string —Return a well formatted delete file link with a custom delete page
generateDirectDownloadLink()
Generate a direct download link
public
generateDirectDownloadLink() : string
Return values
string —Return a well formatted direct download link without a custom download page
generateDownloadLink()
Generate a download link
public
generateDownloadLink() : string
Return values
string —Return a well formatted download link with a custom download page
generateFileID()
Generate a Uniqe ID for each uploaded file
public
generateFileID() : string
Return values
string —Return the uinque id hashed using sha1
generateQrCode()
Generate a Qr Code of the download url
public
generateQrCode([string $qr_size = "150x150" ]) : string
Parameters
- $qr_size : string = "150x150"
-
The size ot the qr code image
Return values
string —Return the qr code image url to display
generateUserID()
Generate a User ID for each uploaded file
public
generateUserID([mixed $disable_session = false ]) : string
Parameters
- $disable_session : mixed = false
Return values
string —Return the user id hashed using sha1
getDate()
Get the date of the uploaded file
public
getDate() : int|bool
Return values
int|bool —Return the file last modification time or false if an error occurred
getExtension()
Function to return the file input extension
public
getExtension() : string
Return values
string —Return the uploaded file extenstion as string
getFileID()
Get the unique file id
public
getFileID() : string
Return values
string —Return the uploaded file uniqe id
getFiles()
Function to return an array with all the uploaded files information
public
getFiles() : array<string|int, mixed>
Return values
array<string|int, mixed> —getFileType()
Function that return the uploaded file MIME type
public
getFileType() : string
Return values
string —Return the file MIME type as string
getJSON()
Get all the uploaded file information in JSON
public
getJSON() : string
Return values
string —Return a JSON string that contains the uploaded file information
getLog()
Get a system log message by an array index id
public
getLog(mixed $log_id) : int
Parameters
- $log_id : mixed
-
The logs id to retrive the message
Return values
int —Return the message id to use with the messages array
getLogs()
Get all logs from system log to view them
public
getLogs() : array<string|int, mixed>
Return values
array<string|int, mixed> —Return an array that contains all the logs in class logs system
getMessage()
Function to get a log message using a message index id
public
getMessage(int $index) : string
Parameters
- $index : int
-
The message index from 1 to 14
Return values
string —Return the log message as string
getName()
Return the name of the uploaded file
public
getName() : string
Return values
string —Return the name of the uploaded file as string
getSize()
Return the size of the uploaded file as bytes
public
getSize() : float
Return values
float —Return the uploaded file size as bytes
getTempName()
Return the PHP Generated name for the uploaded file
public
getTempName() : string
Return values
string —return the temp name that PHP generated for uploaded file
getUploadDirFiles()
Return the files from the upload folder to view them
public
getUploadDirFiles() : array<string|int, mixed>|bool
Return values
array<string|int, mixed>|bool —Return an array the contains all files in the upload folder or false if an error occurred
getUserCloud()
public
getUserCloud(mixed $main_upload_folder) : mixed
Parameters
- $main_upload_folder : mixed
Return values
mixed —getUserID()
Get the unique user id for security
public
getUserID() : string
Return values
string —Return the unique user id
hashName()
Return an "SHA1 Hashed File Name" of the uploaded file
public
hashName() : true
Return values
true —Return the file real name using getName() function and hash it using SHA1
isDir()
Check if a directory exist and it is a real directory
public
isDir(string $dir_name) : bool
Parameters
- $dir_name : string
-
The name of the folder you want check if it exist
Return values
bool —Return true if the folder exist or false otherwise
isFile()
Check if a file exist and it is a real file
public
isFile(string $file_name) : bool
Parameters
- $file_name : string
-
The file you want to cheack if it exist
Return values
bool —Return true if the exist or false otherwise
isImage()
Extra Firewall 2: Function to check if uploaded file is an image
public
isImage() : bool
Return values
bool —Return true if the uploaded file is a real image otherwise false
moveFile()
Function to upload file to the server using chunck system
public
moveFile(string $filename) : bool
Parameters
- $filename : string
-
The file name you want to use as uploaded name
Return values
bool —Return true if the file is uploaded or false otherwise
protectFoler()
Function to potect a folder
public
protectFoler(string $folder_name) : void
Parameters
- $folder_name : string
-
The folder name you want protect
Return values
void —sanitize()
Function that helps with input filter and sanitize
public
sanitize(string $value) : string
Parameters
- $value : string
-
The value of the malicious string you want to sanitize
Return values
string —Return the sanitized string
setController()
Function to set the controller when needed
public
setController(string $controller) : void
Parameters
- $controller : string
-
The folder name of folder that contains the json filters and the class file
Return values
void —setFileID()
Set the unique file id manualy when needed
public
setFileID([string $file_id = "" ]) : void
Parameters
- $file_id : string = ""
-
The unique file id that you want
Return values
void —setForbiddenFilter()
Set Forbidden array to a custom list when needed
public
setForbiddenFilter(array<string|int, mixed> $forbidden_array) : void
Parameters
- $forbidden_array : array<string|int, mixed>
-
An array that contains the forbidden file names like php shell names
Example: ["aaa.php", "file.exe"]
Return values
void —setINI()
Set php.ini settings using an array
public
setINI(array<string|int, mixed> $ini_settings) : void
Example: setINI(["file_uploads"=>1])
Parameters
- $ini_settings : array<string|int, mixed>
-
An array the contains the ini settings variables and values
Return values
void —setMaxDimenstion()
Function to set the miximum class image dimenstions to validate them
public
setMaxDimenstion([int $height = null ][, int $width = null ]) : void
Parameters
- $height : int = null
-
The miximum image height
- $width : int = null
-
The miximum image width
Return values
void —setMinDimenstion()
Function to set the minimum class image dimenstions to validate them
public
setMinDimenstion([int $height = null ][, int $width = null ]) : void
Parameters
- $height : int = null
-
The minimum image height
- $width : int = null
-
The minimum image width
Return values
void —setProtectionFilter()
Set Extension array to a custom list when needed
public
setProtectionFilter(array<string|int, mixed> $filter_array) : void
Parameters
- $filter_array : array<string|int, mixed>
-
An array that contains the allowed file extensions
Return values
void —setSiteUrl()
Set the site url manualy when needed to generate links
public
setSiteUrl([string $site_url = "" ]) : void
Parameters
- $site_url : string = ""
-
The site url you want to genearate urls for
Return values
void —setSizeLimit()
Set file size limit when needed
public
setSizeLimit(int $size) : void
Parameters
- $size : int
-
The size you want to limit for each uploaded file
Return values
void —setUpload()
Function to set upload input when needed
public
setUpload(array<string|int, mixed> $upload_input) : void
Parameters
- $upload_input : array<string|int, mixed>
-
An array of the upload file information coming from $_FILES
Return values
void —setUploadController()
Set the upload controller file
public
setUploadController(string $upload_controller) : void
Parameters
- $upload_controller : string
-
The upload controller that contains the factory code like upload.php
Return values
void —setUploadFolder()
Set upload folder when needed
public
setUploadFolder(array<string|int, mixed> $folder_name) : void
Parameters
- $folder_name : array<string|int, mixed>
-
An array contains the upload folder information full path and name
Example: ["folder_name" => "upload", "folder_path" => realpath("upload")]
Return values
void —setUserID()
Set the unique user id manualy when needed
public
setUserID([string $user_id = "" ]) : void
Parameters
- $user_id : string = ""
-
The unique file id that you want
Return values
void —sizeInBytes()
Return any type of readable storage size to bytes
public
sizeInBytes(int $size) : float
Example: 7.2 MB to 7201450
Parameters
- $size : int
-
The readable size that you want to convert to bytes
Return values
float —Return the bytes size as float
upload()
Function to upload the file to the server
public
upload() : bool
Return values
bool —Return true if the file is uploaded or false otherwise
getMime()
Function to get the MIME type using the server
private
getMime() : string
Return values
string —Return the file MIME type as string