<?php
#################################
# #
# Uploader v1.0 #
# Developed by: rEd nEcK * #
# #
#################################
class Uploader{
var $maxsize, $error, $file, $path;
var $validtype = Array(
"image/bmp",
"image/gif",
"image/jpeg",
"image/pjpeg",
"image/png",
"image/x-png",
"image/tiff",
"audio/mpeg",
);
function Uploader($file,$path="Uploads/"){
$this->file = $file; $this->path = $path;
$this->maxsize = (int) ini_get('upload_max_filesize')*1024*1024;
}
function CheckFile(){
if( !file_exists($this->path."/".$this->file["name"]) ){
$this->error = 0;
return true;
}else{
$this->error = 'O arquivo ['.ucwords($this->file["name"]).'] já existe.';
return false;
}
}
function CheckType(){
if( in_array($this->file["type"],$this->validtype) ){
$this->error = 0;
return true;
}else{
$this->error = 'Tipo do arquivo ['.ucwords($this->file["name"]).'] invalido.';
return false;
}
}
function CheckSize(){
if( $this->file["size"] < $this->maxsize ){
$this->error = 0;
return true;
}else{
$this->error = 'O arquivo ['.ucwords($this->file["name"]).'] é muito grande.';
return false;
}
}
function UploadIt(){
if( !$this->CheckFile() ) return false;
if( !$this->CheckType() ) return false;
if( !$this->CheckSize() ) return false;
move_uploaded_file($this->file["tmp_name"],$this->path."/".strtolower($this->file["name"]));
return true;
}
}
function UploadFiles(){
foreach($_FILES as $k=>$v){
if( !empty($_FILES[$k]["name"]) ){
$upload = new Uploader($_FILES[$k]);
if( !$upload->UploadIt() ){
echo "Erro interno: ".$upload->error;
return false;
}
}
}
return true;
}
?>
Modo de usar:
<?php
if( isset( $_GET["Submit"] ) ){
if( UploadFiles() == false ){
echo '<br/><br/><a href="java script:history.go(-1);">Voltar</a>';
}else{
echo '<script type="text/javascript">';
echo ' alert(" Upload Completo. ");';
echo ' window.close();';
echo '</script>';
}
}else{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> Upload::System </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="100%" height="100%" align="center">
<tr>
<td>
<form action="?Submit" method="post" enctype="multipart/form-data">
<table width="100%" border="0">
<tr>
<td align="center"><font size="4" face="Verdana, Arial, Helvetica, sans-serif">Upload</font></td>
</tr>
<tr>
<td width="50%"><input name="upload00" type="file" id="upload00"></td>
</tr>
<tr>
<td><input name="upload01" type="file" id="upload01"></td>
</tr>
<tr>
<td><input name="upload02" type="file" id="upload02"></td>
</tr>
<tr>
<td><input name="upload03" type="file" id="upload03"></td>
</tr>
<tr>
<td><input name="upload04" type="file" id="upload04"></td>
</tr>
<tr>
<td><input name="uploadGo" type="submit" id="uploadGo" value="Upload It !"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
<?php
}
?>
Para adicionar mais campos é só ir adicionando:
PS: trocar o X pelo numero correspondente, ex: o ultimo campo é o upload04, então o proximo será upload05, e assim por diante<input name="upload0x" type="file" id="upload0x">
PS2: o destino padrão é a pasta atual /Uploads, para alterar isso, basta modificar o que está em vermelho
Qualquer duvida só perguntar,
Abraços
Edição feita por: rEd nEcK *, 06/05/2006, 14:58.