Please rate (code review).

<?php //Если форма отправлена if(isset($_FILES['file'])) { //Массив ошибок $error = array(); //Директория для загрузки картинок $dir = 'upload'; //Имя файла $file = $_FILES['file']['name']; //Временная папка $tmp = $_FILES['file']['tmp_name']; //Допустимые расширения $ext = array('jpg','jpeg','gif', 'png', 'bmp'); //Расширение загружаемого файла $ext2 = strtolower(end(explode('.', $file))); //Новое имя файла $new_name = time().'_'.rand(0, 1000).'.'.$ext2; //Смотрим, есть ли ошибки во время загрузки if($_FILES['file']['error'] == 0) { //Проверяем допустимость расширения загружаемого файла if(in_array($ext2, $ext)) { //Проверяем, картинку ли загрузил пользователь if($imginfo = getimagesize($tmp)) { //Проверяем ширину и высоту загружаемого файла if($imginfo[0] < 100 and $imginfo[1] < 100) { //Если нету директории для загрузки картинок, создаем на лету if(!is_dir($dir)) mkdir($dir, 0777); //Перемещаем картинку в нужную папку if(move_uploaded_file($tmp, $dir.'/'.$new_name)) $error[] = 'ФАЙЛ УСПЕШНО ЗАГРУЖЕН'; else $error[] = 'Ошибка: Не удалось загрузить файл'; } else $error[] = 'Ошибка: ширина и высота файла не могут быть больше 100 пикселей'; } else $error[] = 'Ошибка: вы загружаете не картинку'; } else $error[] = 'Ошибка: неверный формат файла'; } else { //Формируем ошибка switch($_FILES['file']['error']) { case 3: $error[] = 'Ошибка: Файл загружен частично'; break; case 4: $error[] = 'Ошибка: Вы не выбрали файл'; break; } } //Выводим ошибки if(count($error) > 0) { foreach($error as $err) echo '<p>'.$err.'</p>'; } } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" name="ok" value="Загрузить" /> </form> 
  • four
    check it out what next :) - Artem
  • Comment on the version of my code? - vinnie
  • one
    Everyone does it their own way, so I will not undertake to rate it because I would do it differently :) As they said in college life, "3 state grade" :)> $ ext = array ('jpg', 'jpeg', 'gif', 'png', 'bmp'); > // Downloadable file extension> $ ext2 = strtolower (end (explode ('.', $ File))); I already wrote about this, that it’s not good, I can download php;) - Artem
  • 2
    You posted it on phpforum.ru, now here :) I can say by code that it’s pretty bad, I can just rename the .php file to the image and load the shell, and in 5 minutes I will get access to the database and all the scripts, so run rewrite - johniek_comp
  • one
    Who told you that I can not upload images! You said on phpforum, and they say here! but you are not listening, post your code and give a link, and watch your website go down :) - johniek_comp

2 answers 2

IMHO, most of the code is useless. In most cases, doing something like:

 try { $image = new Imagick($_FILES['file']['tmp_name']); $image->scaleImage(MAX_WIDTH, MAX_HEIGHT, true); $image->writeImage('image.jpg'); // картинка корректно загружена } catch (ImagickException $ex) { // разбор ошибки } 

    For a beginner - the most it. The main thing is to understand the principle, and then do it globally! =)