Descargar mediante php

santo
17 de Enero del 2005
Hola he credo una pagina en php para poder subir documentos al servidor, i mediante un pequeño codigo php introduzco el nombre su direccion el tiutulo del documento, fecha i autor en una base de datos para luego mostrar-lo en otra pagina. Asta aqui todo bien, me muestra todos los documentos que tengo subidos, pero quando me ponga a hacer el codigo php para que alguien se pueda vajar dicho documento no se como hacer-lo.
Adjunto codigo:

<?php
include"config.php";
// Sentecia SQL.
$_pagi_sql="SELECT * FROM documentos ORDER BY id DESC"; //Consultar les dades de la base de dades comentarios.
//Numero de registres per pagina.(opcional per defecte 20).
$_pagi_cuantos=4;
//Insertem l'escript de paginaci&oacute;. Aquest ja ejecuta la consuta automaticament.
include("paginator.inc.php");
//Llegim i escribim els registres de la pagina actual.
if ($row=mysql_fetch_array($_pagi_result))
{
echo "<table>
<tr>
<td width=90><p class='Estilo6'><u><strong>Autor</strong></u></p></td>
<td width=90><p class='Estilo6'><u><strong>Títol del document</strong></u></p></td>
<td width=90><p class='Estilo6'><u><strong>Data</strong></u></p></td>
<td width=340 colspan='2'><p class='Estilo6'><u><strong>Accions permeses</strong></u></p></td>
</tr>
<tr></tr>
<tr></tr>";
do{
echo "
<tr>
<td><p class='Estilo7'>".$row[4]."</p></td>
<td><p class='Estilo7'>".$row[1]."</p></td>
<td><p class='Estilo7'>".$row[3]."</p></td>
<td><a href=privat/documents/><p class='Estilo6'><b>Descarregar</p></b></a></td><td>
</tr>";
}
while ($row=mysql_fetch_array($_pagi_result));
}
else { echo "<p class='Estilo7'>No hi ha noticies per mostrar</p>";}
//Insertem la barra de navegacio.
echo"<tr></tr><tr><td>&nbsp;&nbsp;</td><td><font face='Geneva, Arial, Helvetica, sans-serif', size='2', color='#999999'>".$_pagi_navegacion."</font></td></tr></table>";
?>

<a href=privat/documents/> <--Que se supone que debo poner aqui para que se descargue el documento seleccionado?

Gracias,

SANTO

Ronald Infante
17 de Enero del 2005
Escucha, debes abrir una etiqueta <a href> que valla a la direccion de tu archivo. Ejemplo, digamos que tu archivo se encuentra en la carpeta Archivos y el archivo se llama documento.txt lo harias asi:

<a href="Archivos/Documento.txt/"><Aqui pones una imagen para pinchar></a>

Quedando en php asi:
<a href="".$Path.""><img src="Imagenes/Download.gif" alt="Descargar" border=0>

tgp
17 de Enero del 2005
<?php
//guarda este archivo con el nombre de downloader fuera de la carpteta donde estaran tus archivos y solo
//cambias el valor de la variable $download_folder por el nombre de tu carpeta

// main configuration--------------------------------------------------------------------------------
$standalone = "true";
$custom_script_name = "downloader.php";
$enable_stats = "true";
$download_stats_file = "./download_stats.txt";
$download_folder = "tucarpeta/"; // aqui pones el path hacia tus archivos
$auto_create_filelist = "true";

// standalone modus configuration----------------------------------------------------------------------
$headline_string = "Archivos Disponibles";
$css_code = <<<CSS_CODE

/* here you can edit the css code */

body { font-family: Tahoma, sans-serif; }
img { border: 0px;
margin-top: 6px; }
h2 { margin-bottom: 30px;
background-color: #eeeeee;
color: #c0c0c0;
letter-spacing: .2em;
padding: .2em;
text-align: center;
border-top: solid #aaaaaa 1px;
border-bottom: solid #aaaaaa 1px; }
table { border-collapse: collapse;
width: 80%;
margin-left: auto;
margin-right: auto;
margin-bottom: 10px;
border: solid #dddddd 1px; }
th { background-color: #dddddd; }
td { padding: 8px 10px 8px 10px;
background-color: #ffffff;
border: solid #dddddd 1px; }
a { text-decoration: none;
font-weight: bold; }
/* css code editing ends here */
CSS_CODE;
// !!! dont't change the position of the operator CSS_CODE and don't write anything behind, it must be alone at the beginning of the line

//################################################################################
//### ####
//### CONFIGURATION - ends here ####
//### ####
//### !!! don't edit anything above, except you know what you are doing !!! ####
//### ####
//################################################################################

$download = $_GET["download"];
$SCRIPT_NAME = getenv("SCRIPT_NAME");

// auto create download filelist-------------------------------------------------------------------------

if ($auto_create_filelist=="true") {
unset($download_filelist);
$current_dir=getcwd();
chdir($download_folder);
$directory=dir("./");
while ($file = $directory->read()) {
if (is_file($file) and $file!=basename($SCRIPT_NAME) and $file!=basename($download_stats_file) and $file!=$custom_script_name) {
$download_filelist[$file][0]=$file;
$download_filelist[$file][1]="application/octet-stream";
}
}
$directory->close();
chdir($current_dir);
}

// download handling-------------------------------------------------------------------------------

if (isset($download)) {
if (isset($download_filelist[$download])) {
$filename=$download_folder.$download_filelist[$download][0];
if (file_exists($filename)) {
if ($enable_stats=="true") {
if (file_exists($download_stats_file) and is_writable($download_stats_file)) {
foreach (file($download_stats_file) as $line) {
$array=explode("|",$line);
$download_stats_file_array[$array[0]]=$array[1];
}
$download_stats_file_array[$download]++;
$handle=fopen($download_stats_file,"w");
foreach ($download_stats_file_array as $value) {
fputs($handle,key($download_stats_file_array)."|".$value."|n");
next($download_stats_file_array);
}
fclose($handle);
}
else { die("Sorry, no downloads avalible."); }
}
header("Content-Type: ".$download_filelist[$download][1]);
header("Content-Disposition: attachment; filename="".basename($filename).""");
readfile($filename);
exit;
}
else { die("Sorry, but the requested file could not be found."); }
}
else { die("Sorry, but the requested download does not exist."); }
}

// initialize global variables-------------------------------------------------------------------------------
$GLOBALS["SCRIPT_NAME"] = getenv("SCRIPT_NAME");
$GLOBALS["enable_stats"] = $enable_stats;
$GLOBALS["download_stats_file"] = $download_stats_file;
$GLOBALS["download_folder"] = $download_folder;
$GLOBALS["auto_create_filelist"] = $auto_create_filelist;
$GLOBALS["download_filelist"] = $download_filelist;

// functions---------------------------------------------------------------------------------------------

function manage_download_stats()
{
$download_stats_file = $GLOBALS["download_stats_file"];
$download_filelist = $GLOBALS["download_filelist"];

// create download stats file if missing-------------------------------------------------
if (!file_exists($download_stats_file)) {
$handle=fopen($download_stats_file,"w");
foreach($download_filelist as $array) {
fputs($handle,key($download_filelist)."|0|n");
next($download_filelist);
}
fclose($handle);
}
// read download stats file-------------------------------------------------------------------
foreach (file($download_stats_file) as $line) {
$array=explode("|",$line);
$download_stats_file_array[$array[0]]=$array[1];
}

return $download_stats_file_array;
}

function print_download_table()
{
$enable_stats = $GLOBALS["enable_stats"];
$download_folder = $GLOBALS["download_folder"];
$auto_create_filelist = $GLOBALS["auto_create_filelist"];
$download_filelist = $GLOBALS["download_filelist"];
$SCRIPT_NAME = $GLOBALS["SCRIPT_NAME"];



// get download stats----------------------------------------------------------------------------
if ($enable_stats=="true") { $download_stats_file_array=manage_download_stats();}

// create download table from download file list------------------------------------
echo "<table><tr><th>file</th>";
if ($auto_create_filelist!="true") { echo "<th>description</th>"; }
echo "<th>filesize</th>";
if ($enable_stats=="true") { echo "<th>downloads</th>"; }
echo "</tr>";
foreach($download_filelist as $download_filelist_array) {
if (!isset($download_stats_file_array[key($download_filelist)])) { $download_stats_file_array[key($download_filelist)]=0; };
echo "<tr><td><a href ="$SCRIPT_NAME?download=".urlencode(key($download_filelist))."" title="click to download">".key($download_filelist)."</a></td>";
if ($auto_create_filelist!="true") { echo "<td>$download_filelist_array[2]</td>"; }
echo "<td>".sprintf("%.1f",@filesize($download_folder.$download_filelist_array[0])/1024)."K</td>";
if ($enable_stats=="true") { echo "<td>".$download_stats_file_array[key($download_filelist)]."</td>"; }
echo "</tr>";
next($download_filelist);
}
echo "</table>";
}

// standalone output------------------------------------------------------------------------------

if ($standalone=="true") {
// header--------------------------------------------------------------------------------------------------------------------------------------------------
echo "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">";
echo "<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >";
echo "<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />";
echo "<meta name="author" content="Till Biedermann" />";
echo "<meta name="description" content="download system" />";
echo "<title>File download powered by TBmnet.de</title>";
echo "<style type="text/css">".$css_code."</style></head><body>";
// body-----------------------------------------------------------------------------------------------------------------------------------------------------
echo "<h2>".$headline_string."</h2>";
print_download_table();
// footer----------------------------------------------------------------------------------------------------------------------------------------------------
echo "<h2><a href="http://validator.w3.org/check/referer" title="valid xhtml1.1"><img src="http://www.w3.org/Icons/valid-xhtml11.png" alt="Valid XHTML 1.1" height="31" width="88" /></a>";
echo "&nbsp;&nbsp;<a href="http://jigsaw.w3.org/css-validator/check/referer" title="valid CSS"><img src="http://jigsaw.w3.org/css-validator/images/vcss.png" alt="Valid CSS!" height="31" width="88" /></a></h2>";
echo "</body></html>";
}

?>

si tenes problemas revisa el archivo ya que el foro quita algunos elementos o cambia otros como " o ' o <?php etc.