Foros del Web » Programando para Internet » PHP »

Subir imagenes

Estas en el tema de Subir imagenes en el foro de PHP en Foros del Web. Quiero subir una imagen a el servidor por medio de php y pensaba utilizar <form enctype="multipart/form-data" action="_URL_" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="1000"> Send this file: ...
  #1 (permalink)  
Antiguo 15/10/2005, 09:05
 
Fecha de Ingreso: marzo-2005
Mensajes: 28
Antigüedad: 19 años, 1 mes
Puntos: 0
Información Subir imagenes

Quiero subir una imagen a el servidor por medio de php y pensaba utilizar

<form enctype="multipart/form-data" action="_URL_" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000">
Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>

algo como eso pero en la guia de php me dice k eso es para subir archivos temporales, quiero saber como hacer para subir una imagen y que se quede almacenado en la carpeta que haya especificada para siempre.

Gracias
  #2 (permalink)  
Antiguo 15/10/2005, 09:13
Avatar de nuevo  
Fecha de Ingreso: mayo-2003
Ubicación: Spain
Mensajes: 2.009
Antigüedad: 20 años, 11 meses
Puntos: 2
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
__________________
3w.valenciadjs.com
3w.laislatv.com
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:28.