olegvasil
Эксперт
Дата регистрации:
25.03.2013 22:27:59
Сообщений: 106
вот код формы
<?php
require_once "lib/uoload_image_class.php";
require_once "lib/upload_text_class.php";
if($_POST["upload"]){
$upload_text = new UploadText();
$upload_image = new UploadImage();
$success_text = $upload_text->uploadFile($_FILES["text"]);
$success_image = $upload_image->uploadFile($_FILES["image"]);
}
?>
<html>
<head>
<title>Загрузка файлов</title>
</head>
<body>
<?php
if($_POST["upload"]){
if($success_text) echo "Текстовый файл успешно загруженю"."<br/>";
else echo "Ошибка загрузки текстовго файла"."<br/>";
if($success_image) echo "Изображение успешно загружено"."<br/>";
else echo "Ошибка загрузки изображения"."<br/>";
}
?>
<form name = "myform" action = "index.php" method = "post" enctype = "multipart/form-data">
<table>
<tr>
<td>Изображение...</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>
код скрипта
<?php
abstract class Upload {
protected $dir;
protected $mime_types;
public static function uploadFile($file){
if(!self::isSecurity($file)) return false;
$uploadfile = $this->dir."/".$file["name"];
return move_uploaded_file($file["tmp_name"],$uploadfile);
}
protected static 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;
}
}
?>
<?php
require_once "upload_class.php";
class UploadText extends Upload{
protected $dir = "text";
protected $mime_types = array("text/plain"
}
?>
<?php
require_once "upload_class.php";
class UploadImage extends Upload{
protected $dir = "images";
protected $mime_types = array("image/png","image/jpg","image/gif"
}
?>