Ver Mensaje Individual
  #1 (permalink)  
Antiguo 11/07/2012, 11:10
rpichinu
 
Fecha de Ingreso: noviembre-2010
Mensajes: 208
Antigüedad: 13 años, 5 meses
Puntos: 3
Conservar la extension de imagen al cambiarla (Administrador de Imagenes)

Que tal amigos:

Tengo un pequeño administrador de Imagenes. donde subo imagenes, puedo eliminar del listado donde despliego las imagenes al presionar en un input radio, y tambien logro intercambiar imagenes......pero al intercambiar la imagen puede que no conserve la extension y la cambie.

ejemplo. imagen.jp intercambiar por imagen.png
mi script dejaria las 2 como jpg. Y lo que quiero que al intercambiarlas ambas conserven su extension.

admin_imagen.php

Código PHP:
require("upload.php");

$status = "";
if ($_POST["action"] == "upload") {
    $fupload = new Upload();
    $fupload->setPath("../banner");   
    $fupload->setFile("archivo");
    $fupload->isImage(true);
    $fupload->save();
    
    $status = $fupload->message;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../css/hojaEstilos.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <table width="413" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="413" height="40" ><h1 align="center">Administrador de Banners</h1> </td>
          </tr>
        
        <tr>
            <td class="text">Banner Actual:</td>
          </tr>
        <tr>
        <td><img src="../banner/banner.jpg" width='500' height='80' /> </td>
        </tr>

          <tr>
            <td class="text">Por favor seleccione el archivo a subir:</td>
          </tr>
          <tr>
  
                <!-- Formulario para subir banners -->            
        <form action="admin_imagen.php" method="post" enctype="multipart/form-data">
                <td class="text">
                  <input name="archivo" type="file" class="casilla" id="archivo" size="35" />
                  <input name="enviar" type="submit" class="boton" id="enviar" value="Subir Img" />
              <input name="action" type="hidden" value="upload" />
                </td>
        </form><!-- FIN formulario -->

          </tr>
          <tr>
            <td class="text" style="color:#990000"><?php echo $status?></td>
          </tr>
          <tr>
            <td height="30" class="subtitulo">Listado de Archivos Subidos </td>
          </tr>
          <tr>
  
         <td class="infsub">
     
        <?php 
        
echo "<form action='' method='post'>";
        if (
$gestor opendir('../banner'))
         {
           while (
false !== ($arch readdir($gestor)))
         {
          
$i=0;
              if (
$arch != "." && $arch != ".." && $arch != "banner.jpg")
             {
                echo 
"Eliminar      <input type=radio name='delete".$i."' value='../banner/".$arch."'>
                  Update Banner <input type=radio name='update"
.$i."' value='../banner/".$arch."'>
                               <img src='../banner/"
.$arch."' width='500' height='80'/>"."<br />";
            
                if( 
$_POST["delete".$i] == "../banner/".$arch )
                 {
                     echo 
"<p> NOTA: Eliminando Imagen</p> ";
                     
unlink($_POST["delete".$i]);
                    
header("location:admin_banners.php");
                  }
//fin if
                
                
if( $_POST["update".$i] == "../banner/".$arch  )
                {
                    echo 
"<p> NOTA: Cambiando a Banner Actual</p> ";
                    
copy($_POST['update'.$i],'../banner/bannerxxxxx.jpg');
                    
rename('../banner/banner.jpg',$_POST['update'.$i]);
                    
rename('../banner/bannerxxxxx.jpg','../banner/banner.jpg');    
                    
header("location:admin_banners.php");

                }                             
                     }
//fin if
             
$i++;   
                 }
//fin while
            
echo "<br />";
           echo 
"<input type='submit' value='Eliminar' />";
        echo 
"<input type='submit' value='Update' />";
           
closedir($gestor);
        }
//fin if mayor
        
echo "</form>";
    
?>
        <p align="center"> <a href="index.php"><img src="../images/volver.gif" > </a></p> 
    </td>
  </tr>
</table>
</body>
</html>

utilizo upload.php que pille en la net


Código PHP:
<?php 

class Upload {
    var 
$maxsize 0;
    var 
$message "";
    var 
$newfile "";
    var 
$newpath "";
    
    var 
$filesize 0;
    var 
$filetype "";
    var 
$filename "";
    var 
$filetemp;
    var 
$fileexte;
    
    var 
$allowed;
    var 
$blocked;
    var 
$isimage;
    
    var 
$isupload;
    
    function 
Upload() {
        
$this->allowed = array("image/bmp","image/gif","image/jpeg","image/pjpeg","image/png","image/x-png");//archivos permitidos
        
$this->blocked = array("php","phtml","php3","php4","js","shtml","pl","py");//archivos NO permitidos
        
$this->message "";
        
$this->isupload false;
    }
    function 
setFile($field) {
        
$this->filesize $_FILES[$field]['size'];//tamano imagen
        
$this->filename $_FILES[$field]['name'];//nombre imagen
        
$this->filetemp $_FILES[$field]['tmp_name'];
        
$this->filetype mime_content_type($this->filetemp);
        
$this->fileexte substr($this->filenamestrrpos($this->filename'.')+1);
        
        
$this->newfile substr(md5(uniqid(rand())),0,8).".".$this->fileexte;
    }
    function 
setPath($value) {
        
$this->newpath $value;
    }
    function 
setMaxSize($value) {
        
$this->maxsize $value;    
    }
    function 
isImage($value) {
        
$this->isimage $value;
    }
    function 
save() {
        if (
is_uploaded_file($this->filetemp)) {
            
// check if file valid
            
if ($this->filename == "") {
                
$this->message "error";
                
$this->isupload false;
                return 
false;
            }
            
// check max size
            
if ($this->maxsize != 0) {
                if (
$this->filesize $this->maxsize*1024) {
                    
$this->message "Large File Size";
                    
$this->isupload false;
                    return 
false;
                }
            }
            
// check if image
            
if ($this->isimage) {
                
// check dimensions
                
if (!getimagesize($this->filetemp)) {
                    
$this->message "Tipo de archivo Invalido";
                    
$this->isupload false;
                    return 
false;    
                }
                
// check content type
                
if (!in_array($this->filetype$this->allowed)) {
                    
$this->message "Tipo de archivo Invalido";
                    
$this->isupload false;
                    return 
false;
                }
            }
            
//comprobar si el archivo esta permitido
            
if (in_array($this->fileexte$this->blocked)) {
                
$this->message "No permitido- ".$this->fileexte;
                
$this->isupload false;
                return 
false;
            }
                    
            if (
move_uploaded_file($this->filetemp$this->newpath."/".$this->newfile)) {
                
$this->message "Imagen subida con exito!";
                
$this->isupload true;
                return 
true;
            } else {
                
$this->message "Imagen no fue subida..intentente nuevamente";
                
$this->isupload false;
                return 
false;
            }
        } else {
            
$this->message "Imagen no fue subida..intentente nuevamente";
            
$this->isupload false;
            return 
false;
        }
    }    
}
?>

Quiero que me conservee las extensiones....el resto funciona.