Ti faccio un esempio :
Nella cartella "www" del mio PC ho una sottocartella chiamata "tests" e una sotto cartella di "tests" chiamata "worddoc" che contiene dei documenti word : ho xxxx_prova1.doc e xxxx_prova2.doc.
Devi stare attento a non confondere indirizzo internet e file system. Le pagine .php fanno riferimenti a degli indirizzi mentre i documenti word fanno riferimenti al file system ovvero alle path abituali.
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
if (isset($_GET['err'])) {
$err=$_GET['err'];
if ($err == '1') {
echo "File non settato
";
} else {
if ($err == '2') {
echo "File non trovato
";
} else {
echo "Errore sconosciuto
";
} // if ($err == '2')
} // if ($err == '1') {
} // if (! isset($_GET['err'])
//---------- Indirizzo del sito
define("THIS_SITE","http://".$_SERVER['HTTP_HOST']."/");
?>
I documenti word sono in una sotto-cartella di "tests" accessibile
non tramite internet come per un link ma tramite la system file
(ovvero i path tipo c:\pippo\pluto\)
Prova 1
Prova 2
il file "Prova 3" non esiste nella cartella worddoc
Prova 3
</body>
</html>
file word_view.php
codice:
<?php
if (! isset($_GET['doc'])) {
//--------- Errore : nome del documento non settato
header("Location:http://".$_SERVER['referer']."/tests/test_word.php?err=1");
exit;
} // if (! isset($_GET['doc']))
$doc = $_GET['doc'];
//------- La path alla cartella dei documenti word è :
// attenzione non è un indirizzo internet ma una path
// da me DOCUMENT_ROOT = C:/Program Files/EasyPHP/www
$pathWordDoc = $_SERVER["DOCUMENT_ROOT"]."/tests/worddoc/";
//------- Siccome sono furbo ;) sulla pagina precedente non ho
// messo il vero nome del documento.
// nella cartella "worddoc" ho i documenti xxxx_prova1.doc
// xxxx_prova2.doc
$wordDocName = "xxxx_".$doc.".doc";
//------- Path + nome
$realName = $pathWordDoc.$wordDocName;
//------- Testo se il documento esiste nella file system
if (!file_exists($realName)) {
//--------- Errore : documento non esiste
header("Location:http://".$_SERVER['referer']."/tests/test_word.php?err=2");
exit;
} // if (!file_exists($realName))
header("Content-Type: application/word");
header("Content-disposition: inline; filename=${doc}.doc");
include($realName);
exit; ?>