I want to create a dynamic link to a file...basically the info is displayed in a .php template read in from MySQL db. I want to also have a link to the saved text file. Here is some of what I have:

$date=$_POST[Date_Entered];
$shift=$_POST[shift];
$shift_table=$shift."_shift_duties";

$fileDir="TOP".$shift."Shiftlog"
$fileID=.$date."_".$shift."shift.rtf";

and later in the form:

echo"<a href='file:///N:/'$fileDir'/'$fileID>Link to text file</a>(testing)";

Having trouble with the format...is this something that can be done?

Thanks
DS

Dani AI

Generated

Good troubleshooting so far — spotted the missing semicolon and tracked down the stray dot. Two important follow-ups that the thread does not yet cover:

  1. Browser and mapping quirks: embedding file:// links in pages served over HTTP is unreliable (many browsers block them or treat them as mixed content). Also, drive letters are user-specific: a mapped N: on your workstation may not exist for the web server or for other clients.
  2. Safer pattern: let the server deliver the file. That avoids client-side file:// issues and lets you enforce permissions and sanitize input.

A simple, secure workflow:

  • Accept a short identifier (never a full path) from the UI.
  • Map that to a fixed server-side base directory.
  • Use basename() and realpath() to normalise and prevent traversal, and check is_readable().
  • Stream the file with appropriate headers (content-type, content-disposition) so the browser receives it over HTTP.

Example server-side wrapper (place in a file like download.php):

<?php
// download.php - server streams an allowed file
$fileParam = isset($_GET['f']) ? $_GET['f'] : '';
$group = isset($_GET['g']) ? $_GET['g'] : '';
$filename = basename($fileParam);
$baseDir = realpath('/path/to/shift_files'); // change to your server share root
$fullPath = $baseDir . DIRECTORY_SEPARATOR . $group . DIRECTORY_SEPARATOR . $filename;
$real = realpath($fullPath);

if ($real && strpos($real, $baseDir) === 0 && is_readable($real)) {
    header('Content-Type: application/rtf');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    readfile($real);
    exit;
}
http_response_code(404);
?>

Extra tips: HTML href values should be escaped with htmlspecialchars() and any URL parts rawurlencoded. Ensure the webserver account can access the network share (use UNC paths from the server, not per-user mapped drives). See the PHP manual for basename(), realpath(), is_readable(), and readfile() for details: basename (realpath) (is_readable) (readfile).

Recommended Answers

All 4 Replies

Hi.

Here you forgot to put ; ...

$fileDir="TOP".$shift."Shiftlog"

This is working, but I don't know what exactly do you want :

echo"<a href='file:///N:/$fileDir/$fileID'>Link to text file</a>(testing)";

Thanks for pointing that out, still can't get it to work. I am trying to create a link to a file with the following naming convention N:/TOPRShiftlog/2007-12-02_RShift.rtf

the Directory "R" has to be dynamic, being read in with 2 other possibilities, date being read in and the last "R" being dynamic as there are also 2 choices.

I used for date '2007-12-12' and for shift 'R' :

<?php

$date='2007-12-12';
$shift='R';
$shift_table=$shift."_shift_duties";

$fileDir="TOP".$shift."Shiftlog";
$fileID=$date."_".$shift."shift.rtf";

echo"<a href='file:///N:/$fileDir/$fileID'>Link to text file</a>(testing)";

?>

This code return me a link pointing to 'file:///N:/TOPRShiftlog/2007-12-12_Rshift.rtf' ...
This is what you want when I read the previos post.

Thanks for the help. The problem was the "." before the date. It is not being appended to anything so no "." needed.

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.