I want to integrate xupload pro into a existing PHP code
PHP code checks this:
-Maximum file size allowed
-Store uploaded files in dynamic folders
-Assign a unique id / name to each uploaded file
-Check the file extension
But there's no official function that is used to monitor the progress of file upload
Can you help me?
here is the php code:
Code: Select all
private function uploadConfigurableFile($fieldOptions, $file)
{
if (!is_array($fieldOptions) || !is_array($file) || empty($file['name'])) {
return false;
}
$extension = GetFileExtension($file['name']);
$allowedExtensions = array_map('trim', explode(',', $fieldOptions['fieldfiletype']));
$allowedExtensions = array_map('isc_strtolower', $allowedExtensions);
if ($fieldOptions['fieldfiletype'] && !in_array(isc_strtolower($extension), $allowedExtensions)) {
throw new ISC_QUOTE_EXCEPTION(sprintf(GetLang('InvalidFileType'), isc_html_escape($fieldOptions['fieldfiletype'])));
}
// Check that the maximum size is not exceeded
if ($fieldOptions['fieldfilesize'] > 0 && $file['size'] > $fieldOptions['fieldfilesize']*1024) {
throw new ISC_QUOTE_EXCEPTION(sprintf(GetLang('InvalidFileSize'), $fieldOptions['fieldfilesize']));
}
// Store the uploaded files in our configured products directory
$uploadDirectory = ISC_BASE_PATH.'/'.GetConfig('ImageDirectory').'/configured_products_tmp/';
if (empty($file['existingPath'])) {
/**
* @todo Implement temporary sharing/storage location with automatic pruning.
*/
$fileName = $fieldOptions['productfieldid'].'_'.md5(uniqid()).'.'.$extension;
if (!move_uploaded_file($file['tmp_name'], $uploadDirectory.$fileName)) {
throw new ISC_QUOTE_EXCEPTION(getLang('CanNotUploadFile'));
}
}
else {
$fileName = basename($file['existingPath']);
}
// If we've just uploaded an image, we need to perform a bit of additional validation
// to ensure it's not someone uploading bogus images.
$imageExtensions = array(
'gif',
'png',
'jpg',
'jpeg',
'jpe',
'tiff',
'bmp'
);
if (in_array($extension, $imageExtensions)) {
// Check a list of known MIME types to establish the type of image we're uploading
switch(isc_strtolower($file['type'])) {
case 'image/gif':
$imageType = IMAGETYPE_GIF;
break;
case 'image/jpg':
case 'image/x-jpeg':
case 'image/x-jpg':
case 'image/jpeg':
case 'image/pjpeg':
case 'image/jpg':
$imageType = IMAGETYPE_JPEG;
break;
case 'image/png':
case 'image/x-png':
$imageType = IMAGETYPE_PNG;
break;
case 'image/bmp':
$imageType = IMAGETYPE_BMP;
break;
case 'image/tiff':
$imageType = IMAGETYPE_TIFF_II;
break;
default:
$imageType = 0;
}
$imageDimensions = getimagesize($uploadDirectory.$fileName);
if (!is_array($imageDimensions) || $imageDimensions[2] != $imageType) {
@unlink($uploadDirectory.$fileName);
throw new ISC_QUOTE_EXCEPTION(GetLang('InvalidImageFile'));
return false;
}
}
return $fileName;
}
public function applyConfiguration($configuration)
{
$configurableFields = $this->getConfigurableOptions();
// Get the current configuration
$existingConfiguration = $this->configuration;
$newConfiguration = array();
foreach ($configurableFields as $fieldId => $field) {
if ($field['fieldrequired'] && empty($configuration[$fieldId]) &&
empty($existingConfiguration[$fieldId])) {
throw new ISC_QUOTE_EXCEPTION(getLang('EnterRequiredField'));
}
if ($field['fieldtype'] == 'file') {
// Field was empty, use existing value
if (empty($configuration[$fieldId]['name']) && !empty($existingConfiguration[$fieldId])) {
$newConfiguration[$fieldId] = $existingConfiguration[$fieldId];
continue;
}
else if(empty($configuration[$fieldId]['name'])) {
continue;
}
// If we can't save the uploaded file, throw back an error
$uploadedFile = $this->uploadConfigurableFile($field, $configuration[$fieldId]);
if ($uploadedFile === false) {
throw new ISC_QUOTE_EXCEPTION(getLang('CanNotUploadFile'));
}
// If there was an existing file, delete it
if (isset($existingConfiguration[$fieldId]['value']) &&
$existingConfiguration[$fieldId]['value'] != $uploadedFile) {
$this->deleteConfigurableFile($fieldId, true);
}
$newConfiguration[$fieldId] = array(
'type' => $field['fieldtype'],
'name' => $field['fieldname'],
'fileType' => $configuration[$fieldId]['type'],
'fileOriginalName' => $configuration[$fieldId]['name'],
'value' => $uploadedFile,
);
continue;
}
elseif ($field['fieldtype'] == 'select') {
$newConfiguration[$fieldId] = array(
'type' => $field['fieldtype'],
'name' => $field['fieldname'],
'selectOptions' => $field['fieldselectoptions'],
'value' => $configuration[$fieldId],
);
continue;
}
if (!isset($configuration[$fieldId])) {
continue;
}
$value = $configuration[$fieldId];
$newConfiguration[$fieldId] = array(
'type' => $field['fieldtype'],
'name' => $field['fieldname'],
'value' => $configuration[$fieldId],
);
}
// Store the finalized configuration for this product
$this->setConfiguration($newConfiguration);
return $this;
}