Foros del Web » Programando para Internet » PHP »

Detectar variable

Estas en el tema de Detectar variable en el foro de PHP en Foros del Web. Buenas a todos. Tengo un script, en donde las variables $picturepath=carpeta de archivos y $picture=archivo. Cuando $picture es una imagen (gif, jpg, etc.), no hay ...
  #1 (permalink)  
Antiguo 27/03/2009, 12:05
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Detectar variable

Buenas a todos.
Tengo un script, en donde las variables $picturepath=carpeta de archivos y $picture=archivo.
Cuando $picture es una imagen (gif, jpg, etc.), no hay problemas, pero cuando es otro tipo como zip, rar o pdf me gustaria que pulsando sobre ella se pudiera abrir o descargar.
Podrian ayudarme?.
este es el codigo.

if (!empty($picture)){

print "<tr>
<td align=\"center\"><img src=\"$picturepath$picture\"alt=\"$title\"></td>


</tr>";


Gracias por la ayuda.
  #2 (permalink)  
Antiguo 27/03/2009, 12:15
 
Fecha de Ingreso: agosto-2007
Mensajes: 345
Antigüedad: 16 años, 8 meses
Puntos: 21
Respuesta: Detectar variable

Dentro del if haz una comprobación del formato del archivo.

Si es pdf,rar, zip, ...

fuerzas la descarga.

EJ:

si es formato rar

en lugar de mostrar la imagen creas un enlace (pasando el nombre del archivo) asi:

Código PHP:
<a href='./descargar.php?archivo=probando.rar 'descargar </a
y en el archivo descargar.php tienes esto

Código PHP:
Header "Content-Type: application/octet-stream");
    
Header "Content-Length: ".filesize($urlAlArchivo));
    
Header"Content-Disposition: attachment; filename=$archivo");
    
readfile($urlAlArchivo); 
__________________
----
http://www.mascodigo.com
  #3 (permalink)  
Antiguo 27/03/2009, 12:49
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Respuesta: Detectar variable

Este va perfecto
<a href='./descargar.php?archivo=probando.rar '> descargar </a>

Pero este no me funciona, me dá error de sintaxis en el dreamweaver.

Header ( "Content-Type: application/octet-stream");
Header ( "Content-Length: ".filesize($urlAlArchivo));
Header( "Content-Disposition: attachment; filename=$archivo");
readfile($urlAlArchivo);

Por qué puede ser?
Gracias
  #4 (permalink)  
Antiguo 27/03/2009, 12:52
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Detectar variable

Cita:
Iniciado por flatron666 Ver Mensaje
[...] me dá error de sintaxis en el dreamweaver. [...]
y ¿que error si se puede saber???

__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #5 (permalink)  
Antiguo 27/03/2009, 12:58
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Respuesta: Detectar variable

Yo puse este codigo en el dreamweaver.

<?php
Header ( "Content-Type: application/octet-stream");
Header ( "Content-Length: ".filesize($urlAlArchivo));
Header( "Content-Disposition: attachment; filename=$archivo");
readfile($urlAlArchivo);
?>

Y cuando pulso descargar me sale para descargar o abrir el archivo descargar.php

Perdona pero soy novato.
  #6 (permalink)  
Antiguo 27/03/2009, 13:02
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Detectar variable

¿Dónde asignas el valor correcto a la variable $archivo y $urlAlArchivo?
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #7 (permalink)  
Antiguo 27/03/2009, 13:14
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Respuesta: Detectar variable

Este es el script que llama al descargar.php

<?php
require("./NewsSql.inc.php");
$db = new NewsSQL($DBName);
if ((!empty($PR)) && ($Rating>=1)){
$db->set_Rating($newsid,$Rating);
}
$result = $db->getnewsbyid($newsid);
$title = $result[0]["title"];
$content = $result[0]["content"];
$picture = $result[0]["picture"];
$picturepath = "./photo/";
$viewnum = $result[0]["viewnum"];
$sourceurl = $result[0]["sourceurl"];
$source = $result[0]["source"];
$adddate = $result[0]["adddate"];
$db->addhit($viewnum,$newsid);
$Rate = $result[0]["rating"];
$Rate1 = $Rate*10;
$Rate2 = ceil($Rate1);
$Rate = $Rate2/10;

$pre[0] = $newsid-1;

for ($i=1; $i<=$prenumber-1; $i++){
$pre[$i] = $pre[$i-1]-1;
}

$next[0] = $newsid+1;

for ($i=1; $i<=$nextnumber-1; $i++){
$next[$i] = $next[$i-1]+1;
}

?>
<html>
<head>
<title><?php print "$title"; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php print "$front_charset"; ?>">
<link rel="stylesheet" href="./style/style.css" type="text/css">
<script language="JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0">
<?php
include("top.php3");
?>
<table width="412" border="0" cellspacing="1" cellpadding="0" align="center" class="table_01">
<tr>
<td class="menu" bgcolor="#FFFFFF" valign="top" width="410">
<div align="left">
<table width="410" border="0" cellspacing="0" cellpadding="4">
<tr>
<td bgcolor="#F2F2F2" class="menu_in">::<? print "$title"; ?></td>
</tr>
<?php

if (!empty($picture)){

print "<tr>
<a href='./descargar.php?$picturepath$picture '> descargar </a>></td>


</tr>";



Donde $picturepath=ruta y $picture=archivo.
  #8 (permalink)  
Antiguo 27/03/2009, 13:18
Avatar de @ndreX!  
Fecha de Ingreso: abril-2007
Mensajes: 161
Antigüedad: 17 años
Puntos: 4
Respuesta: Detectar variable

Mira el ejemplo que te ponen arriba

<a href='./descargar.php?archivo=probando.rar '> descargar </a>

cambia tu <a href--->

<a href='./descargar.php?archivo=$picturepath$picture'> descargar </a>

Saludos.
__________________
Blog de Andres Ortiz: http://www.axpuntes.com/
  #9 (permalink)  
Antiguo 27/03/2009, 13:21
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Detectar variable

Es que debes pasarlas como variables GET. No como lo estás haciendo. Algo así:
./descargar.php?archivo=$picture&ruta=$picturepath

Después, recuperas esos valores usando $_GET
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #10 (permalink)  
Antiguo 27/03/2009, 13:22
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Respuesta: Detectar variable

Si, ya eso está cambiado, es en el descargar.php donde está el problema.


<?php
Header ( "Content-Type: application/octet-stream");
Header ( "Content-Length: ".filesize($urlAlArchivo));
Header( "Content-Disposition: attachment; filename=$archivo");
readfile($urlAlArchivo);
?>
Saludos
  #11 (permalink)  
Antiguo 27/03/2009, 13:24
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Respuesta: Detectar variable

Amigo David no soy tan iniciado.
Solo necesito como sería el descargar.php.
Gracias
  #12 (permalink)  
Antiguo 27/03/2009, 13:25
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Detectar variable

¿Has cambiando el href como te indiqué? ¿Recuperas los datos en el descargar.php usando $_GET['nombre_variable_pasada_por_URL']?
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #13 (permalink)  
Antiguo 27/03/2009, 13:37
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Respuesta: Detectar variable

Estimado David, he puesto esto en el descargar.php

<?php
$_GET['$picturepath']
$_GET['$picture']
Header ( "Content-Type: application/octet-stream");
Header ( "Content-Length: ".filesize('$picturepath'));
Header( "Content-Disposition: attachment; filename=$picture");
readfile('$picturepath');
?>


y el navegador me dá error en
descargar.php on line 3
Gracias
  #14 (permalink)  
Antiguo 27/03/2009, 13:46
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Detectar variable

Es que no estás asignando las variables. Debería ser:
Código php:
Ver original
  1. $picturepath = $_GET['picturepath'];
  2. $picture = $_GET['picture'];
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #15 (permalink)  
Antiguo 27/03/2009, 13:57
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Respuesta: Detectar variable

Me dá pena de ser tan torpe.
He puesto esto ahora:

$picturepath = $_GET['picturepath'];
$picture = $_GET['picture'];
Header ( "Content-Type: application/octet-stream");
Header ( "Content-Length: ".filesize('$picturepath'));
Header( "Content-Disposition: attachment; filename=$picture");
readfile('$picturepath');
?>

Y ahora me salen estos errores.

Warning: filesize() [function.filesize]: stat failed for $picturepath in descargar.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /usr/home/dirab51.net/dirab51.net/www/news/descargar.php:5) in /usr/home/dirab51.net/dirab51.net/www/news/descargar.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /usr/home/dirab51.net/dirab51.net/www/news/descargar.php:5) in /usr/home/dirab51.net/dirab51.net/www/news/descargar.php on line 6

Warning: readfile($picturepath) [function.readfile]: failed to open stream: No such file or directory in /usr/home/dirab51.net/dirab51.net/www/news/descargar.php on line 7
Gracias por estar ahi.
  #16 (permalink)  
Antiguo 27/03/2009, 14:02
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Detectar variable

Los primeros dos errores significan que ya se envió información al agente de usuario, por lo que ya no se pueden cambiar las cabeceras (revisa la línea 5, es dónde se produce la salida).

El otro error es que no se puede no encuentra el archivo que quieres abrir (me parece que el enlace aún no lo has puesto bien, ¿podrías decirnos cómo tienes el enlace?)
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #17 (permalink)  
Antiguo 27/03/2009, 14:32
 
Fecha de Ingreso: marzo-2009
Mensajes: 55
Antigüedad: 15 años
Puntos: 0
Respuesta: Detectar variable

Ya está, por fin.
esto estaba mal

print "<tr>
<a href='./descargar.php?$picturepath$picture '> descargar </a>></td>

Esto es lo que tenia que poner.
<a href='./$picturepath$picture '> descargar </a>></td>


Gracias por tanta paciencia y ayuda de verdad estoy muy agradecido.
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 13:25.