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 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;
}

endif; 
//exit; 
?>
<DOCTYPE html>
<html lang="en">
<head>
    <title>Download</title>

<script type="text/javascript">
      function myFunc($arg) {

            var filename
            filename = $arg 

            ip = GetUserIpAddr();     
            $stmt = $pdo->prepare("INSERT INTO download (IP_ADDRESS, FILENAME) VALUES (?, ?)");
            $stmt->execute([ip,filename]) ;

            header('Content-Type: octet-stream');
            header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
            header('Pragma: no-cache');
            header('Expires: 0');

            readfile("download/{filename}");
        }
</script>

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="myfunc("<?php echo "{$isoname}";?>)";><img src="images/button_get-the-app.png" alt=""> </a>

Thanks for any help in advance,
Larry

Hey Larry,

This function is a javascript function and you can't run PHP code in javascript. (PHP is server-side & javascript is client-side)

Here is a simple function in javascript I use to open a url:

function openPopup(url,name,h,w){
    var newWindow = window.open(url,name,'height='+h+',width='+w);
    if(window.focus){newWindow.focus();}
}

so you would do:

<a href="#" onclick="openPopup('https://www.example.com/fileDownload.php?file=<?php echo "{$isoname}";?>', 'window name',250,250);";><img src="images/button_get-the-app.png" alt=""> </a>

Then you need to have a php page at the end of the URL that will open and send the file with the appropriate headers.

What if I just made it an external function similar to those called at the start of the page?

You just can't physically do it that way, the PHP runs on the web server and outputs a HTML page to the browser.

So the whole script has already ran and sent the HTML code to the users browser and he has a download page, at this point you can't run a PHP function as PHP is on the web server.

You have to send javascript code along inside the webpage that will send another request to the webserver to get the file to download. Which is what the openPopup function I included before will do.

Its like being on the Microsoft website download page - you can't download the file from your own computer - it's not there. You have to make another connection to the web server to send you the file. The PHP file it connects to doesn't have to be complicated, just verify the person connecting is allowed to download the file and get PHP to read the file from disk and output it.

Here is a simple example I found of outputing an image in PHP:

<?php
$file = '../image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
?>

You save that as it's own PHP file like download.php then when you access it in the browser, the browser gets the image. You are trying to do the same concept for an ISO image. Quickly looking around I think for an iso image you would use header('Content-Type: application/octet-stream'); so when you click the download link - javascript sends a request to a PHP page that reads the ISO image from the disk and gives it to the user.

@Biiim - If that's the case, then how come this works?
<a href="<?php echo "{$iso}";?>"> ("<?php echo "{$iso}";?>"> resolves to download/xxxx.iso)

If you are getting the iso image when you just put the filename into the url like mysite.com/downloads/someiso.iso then your webserver, like Apache, is just serving the file to the user. It works but I don't know of a way through Apache to control who can download it (maybe it does exist but I dont know of it). If you do it like that anyone who can paste the URL mysite.com/downloads/someiso.iso into the browser can download the ISO.

in the PHP method I put earlier, the iso files can be on a local directory not available on the internet and you can control access to them using PHP. if the ISO's are public and you don't mind who gets them then putting them in a web directory would be an easy way of doing it. You can monitor access logs in Apache so you can see to some degree who is accessing the files (IP addresses & site cookies etc.). I think you could control it a lot better with PHP though.

Be a part of the DaniWeb community

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