I'm trying to pass a filename to a function but don't know how to call the function from a click on a link. The top of my index.php is:

<?php
$filename = NULL;
session_start();
// start of script every time.

//  setup a path for all of your canned php scripts
$php_scripts = '/home/larry/web/test/php/'; // a folder above the web accessible tree
//  load the pdo connection module  
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';
require $php_scripts . 'mydloader.php';

//*******************************
//   Begin the script here
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone")):
{
    echo "Failed to connect to database" ;
    exit;
}
else:
{
    $stmt = $pdo->prepare("INSERT INTO download (IP_ADDRESS, FILENAME) VALUES (?, ?)");
    $stmt->execute([$ip,$filename]) ;
}
endif;
?>

<DOCTYPE html>

I want to change it from:

<a href="<?php echo "/{$deb}";?>"><img src="images/button_download.png" alt=""></a>

Which did a direct download of the file to be able to call a function in " require $php_scripts . 'mydloader.php'; " so I can make an entry in the server database and download the file but I have no idea where to start.

Dani AI

Generated

Direct linking to a file will bypass any server-side logging. A common, reliable pattern is to route the click to a small server-side handler (for example download.php) that: validates the requested resource, writes the download row to the database, then streams the file (or issues an internal redirect). That keeps logging and delivery atomic and lets existing modules (PDO, IP helper, etc.) be reused inside one place.

Example handler (minimal, original example):

<?php
// download.php (example)
require __DIR__ . '/bootstrap.php'; // load $pdo, session, config

if (empty($_GET['f'])) {
    http_response_code(400);
    exit;
}

$file = basename($_GET['f']);              // prevents ../ paths
$filesDir = '/home/larry/web/test/files/';
$full = $filesDir . $file;

if (!is_file($full) || !is_readable($full)) {
    http_response_code(404);
    exit;
}

// capture IP (adjust if behind proxies)
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];

// log download (named placeholders shown)
$sql = 'INSERT INTO downloads (ip_address, file_name, downloaded_at) VALUES (:ip, :file, NOW())';
$sth = $pdo->prepare($sql);
$sth->execute([':ip' => $ip, ':file' => $file]);

// stream to client
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . rawurldecode($file) . '"');
header('Content-Length: ' . filesize($full));
readfile($full);
exit;

Example link that points at the handler:

<a href="/download.php?f=manual-v1.2.zip"><img src="/images/button_download.png" alt="Download"></a>

Cautions and improvements: never trust raw filenames — prefer numeric IDs that map to canonical paths in a DB table, or use a whitelist. Use basename() and absolute base directory checks to avoid traversal. For large files prefer webserver delivery (X-Sendfile / X-Accel-Redirect) to avoid PHP memory/time limits. Consider tokenized, time-limited links if files must remain private. If mydloader.php already contains the logging/serving routine, include or call it from the download handler rather than linking files directly — that preserves the logging step mentioned by and answers the duplicate/related concern noted.

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.