Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- - File Upload
- - Allows the upload of a file with specified (or no) limitations.
- - Returns 0 on success
- - Returns 1 if overwrite is disabled and file already exists
- - Returns 2 if it does not meet any valid extensions
- - Returns 3 if a max file size is set and this file exceeds it
- - Returns 4 if width is smaller than min width or height is smaller than min height
- - Returns 5 if width is larger than max width or height is larger than max height
- - Returns 6 if an unexpected error occurred and the file couldn't be uploaded
- $file - $_FILES['filename']; from a form with input type="file" and name="filename"
- $newLocation - The directory relative to the location of this script to move the file to,
- directory will be created if it does not exist already.
- $newName - The new name of the file without the extension (eg. "image")
- $validExtensions - Array containing a list of valid extensions, file will be blocked if it does
- not have any one of these extensions (set it to null for no limits on extensions)
- $overwrite - Overwrite the file if it already exists in this place and as this name?
- $isImage - If only images can be uploaded, set this to true.
- $minImgWidth - The minimum width the image can be. Set to 0 for no minimum width.
- $minImgHeight - The minimuim height the image can be. Set to 0 for no minimum height.
- $maxImgWidth - The maximum width the image can be. Set to 0 for no maximum width.
- $maxImgHeight - The maximum height the image can be. Set to 0 for no maximum height.
- $maxSize - Max size the file can be in bytes, set to 0 for unlimited.
- */
- public function upload($file, $newLocation, $newName, $validExtensions = null, $overwrite = true, $isImage = false, $minImgWidth = 0, $minImgHeight = 0, $maxImgWidth = 0, $maxImgHeight = 0, $maxSize = 8388608) {
- $fileType = pathinfo($file['name'], PATHINFO_EXTENSION);
- $targFile = $newLocation . $newName . "." . $fileType;
- $failed = false;
- if (!$overwrite && file_exists($targFile)) {
- return 1;
- $failed = true;
- }
- if (!$failed) {
- if ($validExtensions != null) {
- $validExt = false;
- foreach ($validExtensions as $validExtension) {
- if ($fileType == $validExtension)
- $validExt = true;
- }
- if (!$validExt) {
- return 2;
- $failed = true;
- }
- }
- if (!$failed) {
- if ($maxSize > 0) {
- if ($file['size'] > $maxSize) {
- return 3;
- $failed = true;
- }
- }
- if (!$failed) {
- if ($isImage) {
- list($width, $height) = getimagesize($file['tmp_name']);
- if ($minImgWidth > 0) {
- if ($width < $minImgWidth) {
- return 4;
- $failed = true;
- }
- }
- if ($minImgHeight > 0) {
- if (!$failed) {
- if ($height < $minImgHeight) {
- return 4;
- $failed = true;
- }
- }
- }
- if ($maxImgWidth > 0) {
- if (!$failed) {
- if ($width > $maxImgWidth) {
- return 5;
- $failed = true;
- }
- }
- }
- if ($maxImgHeight > 0) {
- if (!$failed) {
- if ($height > $maxImgHeight) {
- return 5;
- $failed = true;
- }
- }
- }
- }
- if (!$failed) {
- if (!file_exists($newLocation))
- mkdir($newLocation);
- if (move_uploaded_file($file['tmp_name'], $targFile))
- return 0;
- else
- return 6;
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment