Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * krs1911@gmail.com
- */
- /**
- * You can add more file inputs by just copying & pase line
- * <input name='p1' type='file' accept='<?= implode(',', $settings['ext']); ?>' />
- * remember about changing the field name eg. p2, p3, p4, ...
- */
- /* ----------------------------------------------- */
- /* SETTINGS */
- /* ----------------------------------------------- */
- $settings = array(
- //Maximum file size defined in Bytes. However it depends on what values are set for directives in php.ini: upload_max_filesize, post_max_size and memory_limit
- 'file_maxsize' => 5000000,
- // Accepted extensions
- 'ext' => array(
- 'image/gif'
- ),
- // Directory where files should be placed. Note this is relative path, so folder images/ will be serached in the same location as script execution
- 'dir' => 'images/',
- 'handleUps' => true, // Upload enabled/disabled -> options: true/false
- 'createDirIfNoExist' => true, // Create output directory (specified at 'dir') when doesn't exist. Options true/false
- 'allowNoFile' => false // Allow to execute script if no file has been selected for at least one file field. Options true/false
- );
- /* ----------------------------------------------- */
- /* NO MORE FURTHER EDITING NEEDED */
- /* ----------------------------------------------- */
- try
- {
- // Check if file handling on and ifdata has been sent
- if ($settings['handleUps'] && array_key_exists('send', $_POST))
- {
- // yes - proceed
- $errors = array();
- // Check if files has been sent without any problems
- foreach ($_FILES as $fieldname => $fileprop)
- {
- // Check file error
- if ($fileprop != UPLOAD_ERR_OK)
- {
- switch ($fileprop['error'])
- {
- case UPLOAD_ERR_INI_SIZE:
- case UPLOAD_ERR_FORM_SIZE:
- $errors[] = 'File '.$fileprop['name'].' is too big (maximum size is '.sizeFormat($settings['file_maxsize'], 'MB').')';
- break;
- case UPLOAD_ERR_PARTIAL:
- case UPLOAD_ERR_NO_TMP_DIR:
- case UPLOAD_ERR_CANT_WRITE:
- $errors[] = 'File '.$fileprop['name'].' wasn\'t succesfully uploaded.';
- break;
- case UPLOAD_ERR_NO_FILE:
- if (!$settings['allowNoFile'])
- {
- $errors[] = 'No file has been selected at field '.$fieldname.'.';
- }
- break;
- case UPLOAD_ERR_EXTENSION:
- $errors[] = 'File '.$fileprop['name'].' extension is not correct.';
- break;
- }
- }
- }
- // If there are errors -> do not proceed
- if (!empty($errors))
- {
- throw new Exception(implode('<br />', $errors));
- }
- // Create target directory if not exists and if script allowed to
- if (!file_exists($settings['dir']))
- {
- if (!$settings['createDirIfNoExist'])
- {
- throw new Exception('Target directory does not exists.');
- }
- if (!mkdir($settings['dir'], 0777))
- {
- throw new Exception('Target directory error.');
- }
- }
- // Continue with checking size, veryfing extensions and moving files to te proper directory
- $filesExt = array();
- foreach ($_FILES as $filename => $fileprop)
- {
- try
- {
- // If file is uploaded one
- if (!is_uploaded_file($fileprop['tmp_name']))
- {
- throw new Exception('There was a problem with handling '.$fileprop['name'].' file.');
- }
- // Filesize
- if ($fileprop['size'] > $settings['file_maxsize'])
- {
- throw new Exception('File '.$fileprop['name'].' is too big (maximum size is '.sizeFormat($settings['file_maxsize']).').');
- }
- // Check claimed extension
- if (!in_array($fileprop['type'], $settings['ext']))
- {
- throw new Exception('File '.$fileprop['name'].' has inapropriate extension.');
- }
- // Verify extension
- if (class_exists('finfo') && floatval(phpversion()) >= 5.3)
- {
- $finfo = finfo_open(FILEINFO_MIME_TYPE);
- $type = finfo_file($finfo, $fileprop['tmp_name']);
- }
- else if (function_exists('exif_imagetype'))
- {
- $type = exif_imagetype($fileprop['tmp_name']);
- }
- else
- {
- $type = $fileprop['type'];
- }
- if (!in_array($type, $settings['ext']))
- {
- throw new Exception('File '.$fileprop['name'].' has inappropriate extension.');
- }
- // Save ext
- $filesExt[$filename] = end(explode('/', $type));
- }
- catch (Exception $ex)
- {
- $errors[] = $ex->getMessage();
- }
- }
- // Save files only if there are no errors
- if (!empty($errors))
- {
- throw new Exception(implode('<br />', $errors));
- }
- foreach ($_FILES as $filename => $fileprop)
- {
- // Move uploaded file
- if (!move_uploaded_file($fileprop['tmp_name'], $settings['dir'].time().'.'.$filesExt[$filename]))
- {
- throw new Exception('File '.$fileprop['name'].' couldn\'t be saved.');
- }
- }
- }
- }
- catch (Exception $ex)
- {
- echo '<div style=\'border: 1px solid red; background-color: rgba(255, 0, 0, 0.1); color: red; padding: 15px; text-align: center;\'>'.$ex->getMessage().'</div>';
- }
- function sizeFormat($size, $format)
- {
- switch ($format)
- {
- case 'GB': $size /= 1000;
- case 'MB': $size /= 1000;
- case 'KB': $size /= 1000;
- }
- return $size.' '.$format;
- }
- ?>
- <form method='post' enctype='multipart/form-data'>
- <input name='MAX_FILE_SIZE' type='hidden' value='<?= $settings['file_maxsize']; ?>' />
- <input name='p1' type='file' accept='<?= implode(',', $settings['ext']); ?>' />
- <input name='send' type='submit' value='Send' />
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement