Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
2.3 kB
2
Indexable
Never
function is_valid_type($file) {
                $valid_types = array('image/jpeg', 'image/png');
                if (in_array($file['type'], $valid_types))
                    return 1;
                return 0;
            }
            if ( $_FILES['image'] ) {
                $TARGET_PATH    = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/';
                $image          = $_FILES['image'];
                $type           = pathinfo($image['name'], PATHINFO_EXTENSION);
                $image['name']  = 'user'.$user['id'].'_'.time().'.'.$type;
                $TARGET_PATH   .= $image['name'];
                if ( !is_valid_type($image) ) {
                    $_SESSION['error'] = '<div class="alert">
                    <img src="/images/icons/cross.png" width="16" height="16" alt=""> Вы можете загружать файлы jpeg, png
                    </div>
                    <div class="alert_bottom"></div>';
                    header('Location: ?act=avatar');
                    exit;
                }
                if ( move_uploaded_file($image['tmp_name'], $TARGET_PATH) ) {
                    if ( !$avatar ) {
                        mysql_query('INSERT INTO `avatars` (`user`, `avatar`) VALUES ('.$user['id'].', "'.$image['name'].'")');
                    } else {
                        mysql_query('UPDATE `avatars` SET `avatar` = "'.$image['name'].'" WHERE `user` = '.$user['id']);
                    }
                    $_SESSION['alert'] = '<div class="alert">
                    <img src="/images/icons/ok.png" width="16" height="16" alt=""> Фото успешно загружено
                    </div>
                    <div class="alert_bottom"></div>';
                    header('Location: ?act=avatar');
                    exit;
                } else {
                    $_SESSION['error'] = '<div class="alert">
                    <img src="/images/icons/cross.png" width="16" height="16" alt=""> Невозможно загрузить файл
                    </div>
                    <div class="alert_bottom"></div>';
                    header('Location: ?act=avatar');
                    exit;
                }
            }