Ver Mensaje Individual
  #9 (permalink)  
Antiguo 01/11/2015, 19:29
Avatar de xfxstudios
xfxstudios
 
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 9 años
Puntos: 263
Respuesta: Forzar descarga de Imagen

podrias condicionar las rutas a una variable enviada, por ejemplo:

enlace con las dos variables:
Código HTML:
Ver original
  1. <a href="testb.php?file=1.jpg&rt=M">Descargar</a>

php que ejecuta la descarga:
Código PHP:
Ver original
  1. <?php
  2.  
  3. if (!isset($_GET['file']) || empty($_GET['file'])) {
  4.  exit();
  5. }
  6.  
  7. //rutas de las imagenes
  8. $CA = $_SERVER['DOCUMENT_ROOT']."/imagenes/slider/";
  9. $CB = $_SERVER['DOCUMENT_ROOT']."/imagenes/paisajes/";
  10. $CC = $_SERVER['DOCUMENT_ROOT']."/imagenes/personas/";
  11.  
  12. //verifico la variable que llega y defino la ruta de la descarga
  13. switch ($_GET['rt']) {
  14.   case 'M':
  15.   $ruta = $CA;
  16.   break;
  17.  
  18.   case 'MA':
  19.   $ruta = $CB;
  20.   break;
  21.  
  22.   case 'MB':
  23.   $ruta = $CC;
  24.   break;
  25.    
  26.   default:
  27.   $ruta = $CA;
  28. }
  29.  
  30. $root = $ruta;//asigno la ruta donde esta ubicada la imagen
  31. $file = basename($_GET['file']);
  32. $path = $root.$file;
  33. $type = '';
  34.  
  35. if (is_file($path)) {
  36.  $size = filesize($path);
  37.  if (function_exists('mime_content_type')) {
  38.  $type = mime_content_type($path);
  39.  } else if (function_exists('finfo_file')) {
  40.  $info = finfo_open(FILEINFO_MIME);
  41.  $type = finfo_file($info, $path);
  42.  finfo_close($info);
  43.  }
  44.  if ($type == '') {
  45.  $type = "application/force-download";
  46.  }
  47.  // Definir headers
  48.  header("Content-Type: $type");
  49.  header("Content-Disposition: attachment; filename=$file");
  50.  header("Content-Transfer-Encoding: binary");
  51.  header("Content-Length: " . $size);
  52.  // Descargar archivo
  53.  readfile($path);
  54. } else {
  55.  die("El archivo no existe.");
  56. }
  57.  
  58. ?>
__________________
[email protected]
HITCEL