Depending on the file format you can not view it directly on the page, but you should link to the download of it.
Displaying files directly on the page
If the file is of type text (txt, xml, html, etc.) you can display it directly on the page. Use a <textarea>
, <pre>
or even <div>
tag with the proper CSS.
Let's look at an example:
.conteudo-texto {
display: block;
unicode-bidi: embed;
font-family: monospace;
white-space: pre-wrap;
width: 100%;
}
echo "<tr><td>Alvara Anexo:</td>";
echo "<td><div class='conteudo-texto'>" . htmlentities($exibe['AlvaraConteudo']) . '</div></td>';
It is also possible to limit the size and place the scroll bar, if necessary, by adding the CSS overflow: scroll;
.
The same principle can be applied if the attachment is an image. Simply display it in place with maximum width and height sizes.
Displaying files in frame
, iframe
or popup
PDFs and some other types of binary files are recognized by browsers and plugins, but should be displayed on a separate page.
New page
To display a PDF on a new page, simply create a link with the target
attribute with _blank
value. The link should point to a PHP page which, in turn, will display the contents of the file.
Example for a PDF:
echo '<a href="visualizar_anexo.php?AlvaraNumero=' . $exibe['AlvaraNumero'])
. '" target="_blank">Abrir anexo</a>';
So the PHP file would have something like:
<?php
if (isset($_GET['AlvaraNumero'])) {
try {
// recupera dados do alvará
$conteudo_arquivo = ...
$tamanho_arquivo = ...
$nome_arquivo = ...
header("Content-length: $tamanho_arquivo");
header("Content-type: application/pdf");
header("Content-disposition: inline; filename=$nome_arquivo");
echo $conteudo_arquivo;
} catch (PDOException $e) {
// tratar erro
}
}
Popup
To display in a popup, just add a Javascript event on the link.
Example:
$('a').click(function() {
w = 600;
h = 600;
x = 10;
y = 10;
window.open(this.href, 'anexo', "resizable=no, toolbar=no, scrollbars=no, menubar=no, status=no, directories=no, width=" + w + ", height=" + h + ", left=" + x + ", top=" + y);
return false; //inibe o clique original para não abrir a nova janela
});
14.02.2014 / 13:35