Problem calling function from download page

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

A few things. You can add a log file that is appended to see what happens and trace through events to see if a method is called.

Based on the quoted line, is there also a JavaScript function you are invoking?
Open your browser tools and watch for errors.

I’m a relative newbie. Is there a better way to call the function when the button is clicked?

You could go this way with it,

<?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='{$l_filename}'");
    header('Pragma: no-cache');
    header('Expires: 0');
    
    readfile("download/'$l_filename'"); 
    }
}
function getUserIpAddr() { 
		if(!empty($_SERVER['HTTP_CLIENT_IP'])) { 
			$ip = $_SERVER['HTTP_CLIENT_IP']; 
		}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
		}else{ 
			$ip = $_SERVER['REMOTE_ADDR']; 
		} 
		return $ip; 
}
mydloader( getUserIpAddr(), $_GET['f'] );

And in the html:

 <a href="mydloader.php?f=<?php echo "{$isoname}";?>">

Thanks for that. I’ll give it a try later. Working on another page right now.

1 Like

It errors because " getUserIpAddr() " has already been declared at the start of index.php.

I didnt know you already had a function for that. So, call yours and remove mine

I finally got back to it. When I run it, I get a “File not found” error and it never gets to the var_dump(). This is what mydloader.php looks like:

<?php
    function mydloader($l_filename)
    {
    $files='/home/larry/web/test/public_html/download/';
    var_dump($ip,$l_filename);
    if( isset( $l_filename ) ) {
        $stmt = $pdo->prepare("INSERT INTO download (IP_ADDRESS, FILENAME) VALUES (?, ?)");
        $stmt->execute([$ip, $l_filename]);
        
        header('Content-Type: octet-stream');
        header("Content-Disposition: attachment; filename='{$l_filename}'");
        header('Pragma: no-cache');
        header('Expires: 0');
        
        readfile('$l_filename'); 
        }
    }

    //mydloader( getUserIpAddr(), $_GET['f'] );

    ?>

Here’s what the html looks like:

" <a href=“mydloader.php?f=<?phpho "{$isoname}";?>”> "

The html looks funky for one thing. Are you getting a 404 error, or a file not found error in php?

What is the <?phpho{$isoname} that is going to also be a problem

It’s a 404 error. Don’t know how that error got into the href. I’ll fix it and get back to you.

In the code it’s <a href=“mydloader.php?f=<?php echo "{$isoname}";?>”>. Don’t know why it didn’t copy correctly. It never gets to the var_dump() in the function.

a 404 means the particular file can’t be found. So is the file in the same directory as the file that calls it?

No, it’s in the download directory. Shouldn’t I have seen the var_dump before it tried to download the file?

The link to the page that processes the file should get you to the file first.
So, if it is in the download directory, it should be more like this, maybe,

 <a href=“downlaod/mydloader.php?f=<?php echo $isoname;?>

And I say maybe because that depends where in the file structure everything is.

mydloader.php is in the php directory, one level above the public_html directory. The download directory is in the public_html directory, as is index.php. I’ll look at the link you provided and see what I can do. If I move mydloader.php to the download directory, can I use .htaccess to restrict access to it?

so the file itself has to be accessible. The files that it has access to do not. Understand what I’m saying?

Not sure I understand. After looking at what you referenced, mydloader.php should work if I set a base path at the start of index.php and add the download directory to the path of the iso.
Does that sound anything near correct?

download
---->mydload.php
public_html
---->index.php

This is what it sounds like the files structure is. If that is the case, mydload.php is not accessible from the web and needs to be moved into the public_html directory. From there it can still access the files that will get downloaded, it just acts as a gateway to get to them.

Got it. I’ll move mydloader.php and let you know how it goes. Thanks.

I moved mydloader.php to the download directory. It took me to a blank page with the correct url but no download or entry into the database table. No 404 error. The download directory is under public_html.

The url it took me to: http://test/download/mydloader.php?f=foxclone34-02.iso

Sponsor our Newsletter | Privacy Policy | Terms of Service