Scrivere in un file word il contenuto di un file

Posted on 01. May, 2006 by Administrator in php, tutorials


Obiettivo
In questo articolo tratteremo come leggere il contenuto di un file di tipo testuale qualsiasi e scriverlo in un file word.
>

Articolo
Quello che ci accingiamo a fare e' una cosa molto semplice: leggiamo il contenuto di un file in una variabile
di nome $htmlFile e poi lo scriviamo in un file word.

L'unica cosa da fare e' settare l'header del file in modo corretto

header ("Content-type: application/msword");
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") and !strstr($_SERVER["HTTP_UA_OS"], "Mac")) {
header ("Content-Disposition: filename=articolo.rtf"); // For IE
header ("Pragma: no-store"); // For IE
} else {
header("Content-Disposition: attachment; filename=articolo.rtf"); // For Other browsers
header ("Pragma: no-cache");

e poi utilizzare la funzione "echo" per stampare sul file i dati letti

<?php
header ("Content-type: application/msword");
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") and !strstr($_SERVER["HTTP_UA_OS"], "Mac")) {
header ("Content-Disposition: filename=articolo.rtf"); // For IE
header ("Pragma: no-store"); // For IE
} else {
header("Content-Disposition: attachment; filename=articolo.rtf"); // For Other browsers
header ("Pragma: no-cache");
}

//puntatore al file da leggere
$handle = fopen($htmlFile, "r");
while (!feof($handle)) {
$dimsize = filesize($htmlFile); //dimensione del file
$buffer = fgets($handle, $dimsize); //legge riga per riga
echo("$buffer\n");//la stampa sul file word
}
fclose($handle);
?> 


Correlati

Leave a reply