Hi, i have successfully stored a link to docx and pdf files into mysql db. I'm trying to retrieve it from db and display it similar to pictures. Now when the user click on it, it should open the file and display it.

If any one have a code sinppet that can do that or something similar, pls help. Thank you...

Dani AI

Generated

A compact, practical plan that builds on suggestions from , and :

Present files as a simple list with a file-type icon (easy to scan) and serve the actual file through a small PHP “serve” script so headers can be controlled. PDFs can be offered inline; Office formats should generally be forced to download or converted to PDF at upload time for consistent in-browser viewing.

Example serve script (safe lookup, MIME detection, inline for PDF, attachment otherwise):

<?php
// serve.php?id=123
if (!isset($_GET['id'])) { http_response_code(400); exit; }
$id = (int)$_GET['id'];

$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8','user','pass', [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('SELECT filename, filepath FROM files WHERE id = ? LIMIT 1');
$stmt->execute([$id]);
$file = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$file) { http_response_code(404); exit; }

$full = realpath($file['filepath']);
$base = realpath('/var/www/uploads');
if (!$full || strpos($full, $base) !== 0 || !is_file($full)) { http_response_code(403); exit; }

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $full) ?: 'application/octet-stream';
finfo_close($finfo);

$basename = basename($file['filename']);
$size = filesize($full);

if ($mime === 'application/pdf') {
  header('Content-Type: application/pdf');
  header('Content-Disposition: inline; filename="'.$basename.'"');
} else {
  header('Content-Type: '.$mime);
  header('Content-Disposition: attachment; filename="'.$basename.'"');
}
header('Content-Length: '.$size);
readfile($full);
exit;
?>

A minimal listing UI maps extensions to small icons and links to serve.php?id=.... For inline PDF previews use an <iframe> or <embed> pointing to the same script. For docx, either leave as download or convert to PDF on upload (LibreOffice headless / unoconv) if inline viewing is required. Avoid third-party viewers for sensitive files.

Quick tips and checks: store files outside webroot, use prepared statements, whitelist extensions, detect MIME with finfo, prefer X-Sendfile / X-Accel-Redirect for large files, verify file permissions, and test headers with curl -I. If headers fail, check for early output/whitespace and output buffering.

Recommended Answers

All 5 Replies

Would it work if you would just retrieve the links to the files from you DB, then check what file type it is, and then display an icon for that file type instead of a preview of the file? For example for a .doc file you display a Microsoft Word icon with the file name next to it, and then when the user clicks it he is prompted with a "download file" window.

Alternatively, have one standard.. PDF (which is compatible through many operating systems) thus providing that all documents can be open in the web browser.

Member Avatar for Member #120589

Most files like Word documents will get downloaded when you click their links. There are some online viewers though, but they aren't always accurate in what they display, e.g. Dropbox's viewer - there are certain elements that don't quite look like they should. So, IMO, best to leave files of that type as 'downloads'.

minitauros and diafol, do u have a sample code? Am having trouble with what i have.
Member Avatar for Member #120589

Sample code for what? I suggest not using viewers, just provide links to those files and they'll download.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.