Upload Media files
Example of usage (single file):
include_once DIR . '/uploader.php';
$allowed_mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png');
$maxfilesize = 50000;
$maxfilewidth = 120;
$maxfileheight = 120;
$randomFilename = true;
$uploader = new XoopsMediaUploader('/home/xoops/uploads', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight, $randomFilename);
if ($uploader->fetchMedia('single_file_name')) {
if (!$uploader->upload()) {
echo $uploader->getErrors();
} else {
echo '
File uploaded successfully!
'
echo 'Saved as: ' . $uploader->getSavedFileName() . '
';
echo 'Full path: ' . $uploader->getSavedDestination();
}
} else {
echo $uploader->getErrors();
}
Example of usage (multiple file):
include_once DIR . '/uploader.php';
$allowed_mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/webp');
$maxfilesize = 50000;
$maxfilewidth = 120;
$maxfileheight = 120;
$randomFilename = true;
$uploader = new XoopsMediaUploader('/home/xoops/uploads', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight, $randomFilename);
for ($i = 0; $i < $uploader->countMedia('multiple_file_name'); $i++) {
if ($uploader->fetchMedia('multiple_file_name')) {
if (!$uploader->upload()) {
echo $uploader->getErrors();
} else {
echo '
File uploaded successfully!
'
echo 'Saved as: ' . $uploader->getSavedFileName() . '
';
echo 'Full path: ' . $uploader->getSavedDestination();
}
} else {
echo $uploader->getErrors();
}
}
Methods | ||||||
---|---|---|---|---|---|---|
public
|
__construct(
string $uploadDir,
array $allowedMimeTypes,
int $maxFileSize = 0,
int $maxWidth = null,
int $maxHeight = null,
bool $randomFilename = false,
)
Constructor
Constructor |
# | ||||
public
|
return_bytes($size_str): int
converts memory/file sizes as defined in php.ini to bytes
converts memory/file sizes as defined in php.ini to bytes |
# | ||||
public
|
countMedia(string $media_name): int|false
Count the uploaded files (in case of miltiple upload)
Count the uploaded files (in case of miltiple upload) Parameters
|
# | ||||
public
|
fetchMedia(string $media_name, int $index = null): bool
Fetch the uploaded file
Fetch the uploaded file Parameters
|
# | ||||
public
|
setTargetFileName(string $value)
Set the target filename
Set the target filename |
# | ||||
public
|
setPrefix(string $value)
Set the prefix
Set the prefix |
# | ||||
public
|
getMediaName(): string
Get the uploaded filename
Get the uploaded filename |
# | ||||
public
|
getMediaType(): string
Get the type of the uploaded file
Get the type of the uploaded file |
# | ||||
public
|
getMediaSize(): int
Get the size of the uploaded file
Get the size of the uploaded file |
# | ||||
public
|
getMediaTmpName(): string
Get the temporary name that the uploaded file was stored under
Get the temporary name that the uploaded file was stored under |
# | ||||
public
|
getSavedFileName(): string
Get the saved filename
Get the saved filename |
# | ||||
public
|
getSavedDestination(): string
Get the destination the file is saved to
Get the destination the file is saved to |
# | ||||
public
|
upload(int $chmod = 0644): bool
Check the file and copy it to the destination
Check the file and copy it to the destination |
# | ||||
public
|
_copyFile($chmod): bool
Copy the file to its destination
Copy the file to its destination |
# | ||||
public
|
checkMaxFileSize(): bool
Is the file the right size?
Is the file the right size? |
# | ||||
public
|
checkMaxWidth(): bool
Is the picture the right width?
Is the picture the right width? |
# | ||||
public
|
checkMaxHeight(): bool
Is the picture the right height?
Is the picture the right height? |
# | ||||
public
|
checkMimeType(): bool
Check whether or not the uploaded file type is allowed
Check whether or not the uploaded file type is allowed |
# | ||||
public
|
checkImageType(): bool
Check whether or not the uploaded image type is valid
Check whether or not the uploaded image type is valid |
# | ||||
public
|
sanitizeMultipleExtensions()
Sanitize executable filename with multiple extensions
Sanitize executable filename with multiple extensions |
# | ||||
public
|
setErrors(string $error)
Add an error
Add an error |
# | ||||
public
&
|
getErrors(bool $ashtml = true): array|string
Get generated errors
Get generated errors Parameters
ReturnsArray of array messages OR HTML string |
# | ||||
protected
|
arrayPushIfPositive(array $set, int $value): mixed
Push value onto set.
Used in max file size calculation to eliminate -1 (unlimited) ini values
Push value onto set. Used in max file size calculation to eliminate -1 (unlimited) ini values Parameters
|
# |
Properties | |||
---|---|---|---|
public
|
|
$allowUnknownTypes = false
Flag indicating if unrecognized mimetypes should be allowed (use with precaution ! may lead to security issues )
Flag indicating if unrecognized mimetypes should be allowed (use with precaution ! may lead to security issues ) |
# |
public
|
|
$mediaName
|
# |
public
|
|
$mediaType
|
# |
public
|
|
$mediaSize
|
# |
public
|
|
$mediaTmpName
|
# |
public
|
|
$mediaError
|
# |
public
|
|
$mediaRealType = ''
|
# |
public
|
|
$uploadDir = ''
|
# |
public
|
|
$allowedMimeTypes = []
|
# |
public
|
|
$deniedMimeTypes = ['application/x-httpd-php']
|
# |
public
|
|
$maxFileSize = 0
|
# |
public
|
|
$maxWidth
|
# |
public
|
|
$maxHeight
|
# |
public
|
|
$targetFileName
|
# |
public
|
|
$prefix
|
# |
public
|
|
$errors = []
|
# |
public
|
|
$savedDestination
|
# |
public
|
|
$savedFileName
|
# |
public
|
|
$extensionToMime = []
|
# |
public
|
|
$checkImageType = true
|
# |
public
|
|
$extensionsToBeSanitized = ['php', 'phtml', 'phtm', 'php3', 'php4', 'cgi', 'pl', 'asp', 'php5', 'php7']
|
# |
public
|
|
$imageExtensions = [
1 => 'gif',
2 => 'jpg',
3 => 'png',
4 => 'swf',
5 => 'psd',
6 => 'bmp',
7 => 'tif',
8 => 'tif',
9 => 'jpc',
10 => 'jp2',
11 => 'jpx',
12 => 'jb2',
13 => 'swc',
14 => 'iff',
15 => 'wbmp',
16 => 'xbm',
17 => 'webp',
]
|
# |
public
|
|
$randomFilename = false
|
# |