Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/02/2013, 15:12
Avatar de mager3
mager3
 
Fecha de Ingreso: junio-2012
Ubicación: Medellin
Mensajes: 318
Antigüedad: 11 años, 11 meses
Puntos: 4
Información poder sobrescribir un archivo que se carga al servidor

Hola a todos:

Requiero de un valioso apoyo de ustedes programadores avanzados, he logrado armar este codigo con FAQS de varios foros, es subir una imagen al servidor y cambiarle el tamaño a dicha imagen a un tamaño por defecto . pero no me sobreescribe la imagen si esta ya existe en la carpeta del servidor, arroja error !!! como puedo hacer para que me sobreescriba?

Código PHP:
Ver original
  1. <?php
  2.         include ("conexion.php");
  3.          session_start();
  4.           $documento=$_SESSION['documento_user'];
  5.           echo $documento;
  6.          $dirbase = '';
  7.          $overwrite = "yes"; // Permitir sobreescritura? (yes/no)
  8.          if ($HTTP_POST_FILES['MINIATURA']['name'] != '') {
  9.             $nombre_archivo = $HTTP_POST_FILES['MINIATURA']['name'];
  10.             $tipo_archivo = $HTTP_POST_FILES['MINIATURA']['type'];
  11.             $tamano_archivo = $HTTP_POST_FILES['MINIATURA']['size'];
  12.             $archivo = pathinfo($nombre_archivo);
  13.             $archivo = $archivo['filename'];
  14.             //compruebo si las características del archivo son las que deseo
  15.             $url_mini_no_valida = '';
  16.             $sw = '';
  17.             if (strpos($tipo_archivo, "jpg")) $sw = $tipo_archivo;
  18.             if (strpos($tipo_archivo, "jpeg")) $sw = $tipo_archivo;
  19.             if (strpos($tipo_archivo, "png")) $sw = $tipo_archivo;
  20.             if (strpos($tipo_archivo, "gif")) $sw = $tipo_archivo;
  21.             if ($sw == '') {
  22.                $msj = 'Extensión de archivo no válida: '.$tipo_archivo;
  23.                echo '<script> alert("Tipo de archivo no soportado: '.$msj.'"); </script>';        
  24.                $tpl->assign($data);
  25.                //$tpl->assign('ruta_miniatura', 'http://localhost/phpld22/miniaturas/');
  26.                $content = $tpl->fetch('admin/dir_links_edit.tpl');
  27.                $tpl->assign('content', $content);
  28.                //Clean whitespace
  29.                $tpl->load_filter('output', 'trimwhitespace');
  30.                //Make output
  31.                echo $tpl->fetch('admin/main.tpl');  
  32.                die();
  33.             } else {
  34.                if (move_uploaded_file($HTTP_POST_FILES['MINIATURA']['tmp_name'], $dirbase.'temp/'.$nombre_archivo)) {
  35.                   if (file_exists($dirbase.'miniaturas/_'.$archivo.'.jpg')) {
  36.                      if (!unlink($dirbase.'miniaturas/_'.$archivo.'.jpg')) {
  37.                         echo "fallo al borrar miniatura anterior";
  38.                         die();
  39.                      }
  40.                   }
  41.                   escalar($dirbase.'temp/'.$nombre_archivo, 103,136);
  42.                   if (!unlink($dirbase.'temp/'.$nombre_archivo)) {
  43.                      echo "fallo al borrar imagen original temporal";
  44.                      die();
  45.                   }
  46.                   rename ($dirbase.'temp/_p'.$archivo.'.jpg', $dirbase.'miniaturas/_p'.$archivo.'.jpg');
  47.                   $data['MINIATURA'] = '_p'.$archivo.'.jpg';
  48.                } else {
  49.                   echo "Ocurrió algún error al subir el fichero. No pudo guardarse.";
  50.                }
  51.             }
  52.          }  
  53.  
  54. function escalar($imagen,$ancho,$altura) {
  55.    $prefijo_thumb = "_p";
  56.    $camino_nombre=explode("/",$imagen);
  57.    $nombre=end($camino_nombre);
  58.    $camino=substr($imagen,0,strlen($imagen)-strlen($nombre));
  59.    $archivo = pathinfo($imagen);
  60.    $extension = $archivo['extension'];
  61.    $nombre_archivo = $archivo['filename'];
  62.    
  63.    if (!file_exists($camino.$prefijo_thumb.$nombre)) {
  64.       if (($extension == 'jpeg') || ($extension == 'jpg')) {
  65.          $img = @imagecreatefromjpeg($camino.$nombre);
  66.       } elseif ($extension == 'png') {
  67.          $img = @imagecreatefrompng($camino.$nombre);
  68.       } elseif ($extension == 'gif') {
  69.          $img = @imagecreatefromgif($camino.$nombre);
  70.       }
  71.        if (!$img) { /* Comprobar si ha fallado */
  72.      
  73.          $img = @imagecreatefromjpeg("imagenes/img1.jpg");  
  74.          $datos = getimagesize("imagenes/img1.jpg") or die("Problemas con $camino$nombre<br>");
  75.          $anchura = $ancho;
  76.          $thumb = imagecreatetruecolor($anchura,$altura);
  77.          imagecopyresampled ($thumb, $img, 0, 0, 0, 0, $anchura, $altura, $datos[0], $datos[1]); // calidad
  78.          //imagecopyresized ($thumb, $img, 0, 0, 0, 0, $anchura, $altura, $datos[0], $datos[1]); // rapidez
  79.          imagejpeg($thumb,$camino.$prefijo_thumb.$nombre);
  80.          
  81.        } else {  
  82.          $datos = getimagesize($camino.$nombre) or die("Problemas con $camino$nombre<br>");
  83.          $ratio = ($datos[0] / $ancho);
  84.          $ancho_final = $ancho;
  85.          $alto_final = round($datos[1] / $ratio);
  86.          $escalada = imagecreatetruecolor($ancho_final,$alto_final);
  87.          imagecopyresampled ($escalada, $img, 0, 0, 0, 0, $ancho_final, $alto_final, $datos[0], $datos[1]); // calidad
  88.          //imagecopyresized ($thumb, $img, 0, 0, 0, 0, $anchura, $altura, $datos[0], $datos[1]); // rapidez
  89.          
  90.          $thumb = imagecreatetruecolor($ancho,$altura);
  91.          imagecopyresampled ($thumb, $escalada, 0, 0, 0, 0, $ancho, $altura, $ancho, $altura); // calidad
  92. $_POST['asd'] = 'http://www.minicio.com.ar/miniaturas/'.$prefijo_thumb.$nombre_archivo.'.jpg';
  93.          imagejpeg($thumb,$camino.$prefijo_thumb.$nombre_archivo.'.jpg');
  94.       }
  95.    }
  96. }
  97.  
  98. ?>
  99.  
  100. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  101. <html xmlns="http://www.w3.org/1999/xhtml">
  102. <head>
  103. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  104. <title>Documento sin t&iacute;tulo</title>
  105. </head>
  106.  
  107. <body>
  108.  
  109. <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
  110.   <label>
  111.   <input type="file" name="MINIATURA" id="MINIATURA" />
  112.   </label>
  113.   <p>
  114.     <label>
  115.     <input type="submit" name="enviar" id="enviar" value="Enviar" /> Porfavor para realizar la primera prueba descarga la "Imagen Original".
  116.     </label>
  117.   </p>
  118. </form>
  119. <table width="675" height="312" border="1">
  120.   <tr>
  121.     <td width="329" height="33">Imagen Original: medias 300 x 300</td>
  122.     <td width="330">Imagen Dimencionada: medias 310 x 250</td>
  123.   </tr>
  124.   <tr>
  125.     <td height="228"><img src="http://www.minicio.com.ar/prueba.jpg" /></td>
  126.     <td><img src="<?php echo $_POST['asd']; ?>"/></td>
  127.   </tr>
  128. </table>
  129. </body>
  130. </html>