formulario:
Código PHP:
<html>
<body>
<table align="CENTER" width="500" border="0">
<tr>
<td bgcolor="000099" colspan="2"><div align="center">Sube Tu Imagen / Banner</div></td>
</tr>
<tr>
<td height="10" colspan="2">
<div align="left">
</CENTER>
</td>
</tr>
<tr>
<td height="10" colspan="2"><div align="center">
<?php
include_once("class.upload.inc.php");
if ( $send == "ok" && $_FILES[fichero]['name'] ){
$upload = new upload();
if ($upload -> putFile ("fichero")) {
$imagen = $upload->file_name;
}
}
?>
</div></td>
</tr>
<form action="<?php $PHP_SELF; ?>" method="post" enctype="multipart/form-data"> <tr>
<td colspan="2">
<p align="center">
<input name="fichero" type="file">
</p>
</td>
</tr>
<tr>
<td colspan="2"> <p align="center">
<input name="Submit" type="submit" value="Subir Fichero">
<input name="send" type="hidden" value="ok">
</p></td>
</tr></form>
</table>
</body>
</html>
--------------------------------------------------
classe de upload
class.upload.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 = 25000, $dir = "images",$idioma = "spanish"){//el 25000, son 25k max.upload, y images es la carpeta, modifica a tu gusto
if ( empty ($permiso) ) $permiso = array ("image/pjpeg","image/x-png","image/jpeg","image/png","image/gif");//pueden subir, png, jpg, o 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;
}
}
?>
prueba esta classe de upload