Depending on the file format you cannot display it directly on the page, but you should put a link to the download of the same.
Displaying files directly on the page
If the file is text type (txt, xml, html, etc.) you can display it directly on the page. Use a tag <textarea>
, <pre>
or even in a <div>
with due CSS.
Let’s take 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>';
You can also limit the size and place a scroll bar if necessary by adding 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 a frame
, iframe
or popup
Pdfs and some other binary file types 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 attribute target
worthwhile _blank
. 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 in 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
});
Note that this is a simplified example. You should adjust the selector and parameters as needed.
Frame
The third option is to display the file in a frame
or iframe
. Example:
echo '<iframe src="visualizar_anexo.php?AlvaraNumero=' . $exibe['AlvaraNumero'])
. '"></iframe>';
Add a CSS style to iframe
to leave it at a suitable size.
Forcing the download of the file
If you simply want to force the file to download, just add a link, in a similar way to display on another page.
Example:
echo '<a href="download_anexo.php?AlvaraNumero=' . $exibe['AlvaraNumero'])
. '">Abrir anexo</a>';
The PHP download script will be similar to the file for display, changing only the header Content-disposion
.
Example:
<?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: download; filename=$nome_arquivo");
echo $conteudo_arquivo;
} catch (PDOException $e) {
// tratar erro
}
}
Recovering the mime type automatically
The Mime Type is a standardized constant that tells the browser what kind of content is sent by PHP. In the examples, I used the value application/pdf
fixed in the code.
However, you can use the function finfo_file to recover this value automatically if the file is written in some directory. But you probably won’t want this, so you can use the function finfo_buffer
to get the file type through the contents.
Example:
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->buffer($conteudo_arquivo);
I think that that one question can help.
– rray