В index.php мы принимаем наши файлы через форму, затем проверяем и направляем файлы через подключаемые скрипты (upload_class.php) и направляем их в соответствующие папки (images, text).
upload_class.php
abstract class Upload{
protected $dir; // папка
protected $mime_types; // тип
public function uploadFile($file){ // передаём файл и проверяем на безопасность
if (!$this->isSecurity($file)) return false;
$uploadfile = $this->dir."/".$file["name"];
return move_uploaded_file($file["tmp_name"], $uploadfile);
}
// Проверка на тип
protected function isSecurity($file){
$blacklist = array("php",".phtml",".php3",".php4",".html",".htm");
foreach ($blacklist as $item){
if(preg_match("/$item=\$/i", $file["name"])) return false;
}
$type = $file["type"];
for ($i = 0; $i < count($this->mime_types); $i++){
if ($type == $this->mime_types[$i]) break;
if ($i + 1 == count($this->mime_types)) return false;
}
// проверка на размер
$size = $file["size"];
if ($size > 2048000) return false;
return true;
}
uploadimage_class.php
require_once "upload_class.php";
class UploadImage extends Upload{
protected $dir = "images";
protected $mime_types = array("image/png","image/jpeg","image/gif");
}
uploadtext_class.php
require_once "upload_class.php";
class UploadText extends Upload{
protected $dir = "text";
protected $mime_types = array("text/plain");
}
index.php
<?
require_once "lib/uploadtext_class.php";
require_once "lib/uploadimage_class.php";
if($_POST["upload"]){
$upload_text = new UploadText();
$upload_image = new UploadImage();
$succeess_text = $upload_text->uploadFile($_FILES["text"]);
$succeess_image = $upload_image->uploadFile($_FILES["image"]);
}
?>
<html>
<head>
<title>Загрузка файлов в php</title?>
</head>
<body>
<h1>Загрузка файлов</h1>
<?
if($_POST["upload"]){
if ($succeess_text) echo "Загрузка текстового файла удалась";
else echo "Загрузка текстового файла не удалась";
if ($succeess_image) echo "Загрузка изображений удалась";
else echo "Загрузка изображений не удалась";
}
?>
<form name="myform" action="index.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Images</td>
<td>
<input type="file" name="image"/>
</td>
</tr>
<tr>
<td>Text</td>
<td>
<input type="file" name="text"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="upload" value="Загрузить" />
</td>
</tr>
</table>
</form>
</body>
</html>