yo uso esto... por si no quieres calentarte demasiado...
fichero: classe class.upload.img.inc.php
Código PHP:
<?php
class UpLoad
{
var $archivos_permitidos;
var $dir;
var $max_filesize;
var $idioma;
var $error = 0;
var $file_type;
var $file_name;
var $file_size;
var $archivo;
var $file_path;
var $warning = 0;
function UpLoad ($permiso="" , $max_file = 20000, $dir = "banner",$idioma = "spanish"){
if ( empty ($permiso) ) $permiso = array ("image/pjpeg","image/x-png","image/jpeg","image/png","image/gif");
$this->archivo_permitido = $permiso;
$this->max_filesize = $max_file;
$this->dir = $dir;
$this->idioma = $idioma;
}
function putFile ($file){
$this->file_type = strtok ( $_FILES[$file]['type'], ";");
$this->file_name = $_FILES[$file]['name'];
$this->file_size = $_FILES[$file]['size'];
$this->temp = $_FILES[$file]['tmp_name']; // upload para o diretorio temp
if(!in_array($this->file_type, $this->archivo_permitido))
$this->Error(1);
if ( ($this->file_size <= 0) || ($this->file_size > $this->max_filesize) ) $this->Error(2);
if ($this->error == ""){
$filename = basename ($this->file_name);
if (!empty ($this->dir) )
$this->arquivo = $this->dir."/".$filename;
else
$this->arquivo = $filename;
if (file_exists($this->arquivo)){
srand((double)microtime()*1000000);
$filename = rand(0,20000)."_".$filename;
$new_letras = "";
$leng_letras = 8;
for ($i=1; $i<=$leng_letras; $i++)
{
$abcdario = chr(rand(97,122));
$new_letras .= $abcdario;
}
$filename = "copia_".$new_letras."_".$filename;
if (!empty ($this->dir))
$this->arquivo = $this->dir."/".$filename;
else
$this->arquivo = $filename;
}
if(!is_uploaded_file($this->temp)) $this->Error(3);
if(!@move_uploaded_file($this->temp,$this->arquivo) ) $this->Error(4);
return $this->arquivo;
}
else {
return false;
}
}
function Error($op){
if($this->idioma="spanish"){
switch ($op){
case 0: return; break;
case 1: $this->error = "Error 1: Error tipo de archivo no permitido, jpg , gif , png: $this->file_type."; break;
case 2: $this->error = "Error 2: Error en el tamaño de fichero: $this->file_size Kb. Tamaño Maximo $this->max_filesize."; break;
case 3: $this->error = "Error 3: Error de transferencia de datos: $this->file_name."; break;
case 4: $this->error = "Error 4: Error cuando se copia el fichero al directorio temporal $this->temp para $this->file_name."; break;
case 5: $this->error = "Error 5: Error gd.dll, no esta instalada"; break;
}
}
?> <SCRIPT> alert("<?= $this->error; ?>"); </SCRIPT>
<script languaje="javascript">
setTimeout ("history.back()", 1);
</script>
<?php
exit;
}
}
?>
fin classe class.upload.img.inc.php
funcion de upload
Código PHP:
<?
include_once("class.upload.img.inc.php");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.testo {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
}
.titulo {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bolder;
color:#FFFFFF;
}
.eu {
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
}
-->
</style>
</head>
<body>
<table align="CENTER" width="500" border="0">
<tr>
<td bgcolor="000099" colspan="2"><div align="center" class="titulo">Sube Tu Imagen / Banner</div></td>
</tr>
<tr>
<td height="10" colspan="2">
<div align="left" class="eu">
</CENTER>
</td>
</tr>
<tr>
<td height="10" colspan="2"><div align="center">
<?php
if ( $send == "ok" && $_FILES[userfile]['name'] ){
$upload = new upload();
if ($upload -> putFile ("userfile")) {
print "<div class=\"testo\"><br>El fichero subio correctamente.<br>";
print "Nombre de fichero: ";
print $upload->file_name;
print "<br>Carpeta: ";
print $upload->dir;
print "<br><br></div>";
}
}
?>
</div></td>
</tr>
<form action="<?php $PHP_SELF; ?>" method="post" enctype="multipart/form-data"> <tr>
<td colspan="2">
<p align="center">
<input name="userfile" type="file" class="testo">
</p>
</td>
</tr>
<tr>
<td colspan="2"> <p align="center">
<input name="Submit" type="submit" value="Subir Fichero" class="testo">
<input name="send" type="hidden" value="ok">
</p></td>
</tr></form>
</table>
</body>
</html>