I have a download page that needs to run a function when clicking on a link. The function is supposed to enter a row in a mysql database table and download the selected file. I don’t know if I’ve coded the function incorrectly or I coded the onclick incorrectly. Here’s the start of the page:
<?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;
//exit;
?>
<DOCTYPE html>
Here’s what mydloader.php looks like:
<?php
function mydloader($l_ip, $l_filename)
{
if( isset( $l_filename ) ) {
$stmt = $pdo->prepare("INSERT INTO download (IP_ADDRESS, FILENAME) VALUES (?, ?)");
$stmt->execute( $l_ip, $l_filename );
header('Content-Type: octet-stream');
header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
header('Pragma: no-cache');
header('Expires: 0');
readfile("download/'$l_filename'");
}
}
?>
Here’s the section with the call to the function:
<!-- DOWNLOAD -->
<div id="download" class="stylized">
<div "myform">
<div class="container">
<div class="row">
<div class="download">
<br /><br>
<h1><center>FoxClone Download Page</center></h1>
<?php
$isos = glob('download/*.iso');
$iso = $isos[count($isos) -1];
$isoname = basename($iso);
$md5file = md5_file($iso);
?>
<div class="container">
<div class="divL">
<h3>Get the "<?php echo "{$isoname}";?>" file (approx. 600MB)</h3>
<a href="#" onclick="mydloader($ip,"<?php echo "{$isoname}";?>)";><img src="images/button_get-the-app.png" alt=""> </a>
The database table insert isn’t happening, neither is the download. I have no idea how to test if mydloader.php is ever actually being called.
Thanks in advance for your help,
Larry