My main download page passes an id number for the file to be downloaded. In the mydloader.php (located in the download folder), I have a pdo sql query to set $l_filename to the filename in the database. I need to use $l_filename in <a href to download the file. My code follows:

<?php

$php_scripts = '../../php/';
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';
function mydloader($id=NULL)

{
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data"))
{   
    exit;
}

    $stmt = $pdo->prepare('SELECT filename FROM files WHERE id =?');
    $stmt->execute([$id]);
    $l_filename = $stmt->fetch();

    if( isset( $l_filename ) ) {  
        <a href="">
        $ext = pathinfo($l_filename, PATHINFO_EXTENSION);
        $stmt = $pdo->prepare("INSERT INTO download (address, filename,ip_address) VALUES (?, ?, inet_aton('$ip'))");
        $stmt->execute([$ip, $ext]) ; 

        $test = $pdo->query("SELECT id FROM lookup WHERE INET_ATON('$ip') BETWEEN start AND end ORDER BY start DESC, end DESC");
        $ref = $test->fetchColumn();
        $ref = intval($ref);

        $stmt = $pdo->prepare("UPDATE download SET ref = '$ref' WHERE address = '$ip'");
        $stmt->execute() ;         
       }

    else {
        echo "isset failed";
        }  
}
mydloader($_GET["f"]);
exit;

Thanks in advance

  1. What you are showing seems to be file upload part.
  2. File content function not visible in your script. i.e. move_uploaded_file() to be moved to your folder.
  3. Href link to download file can be rendered in html emebded with php loop of file table.

echo "<a href='http:domain/contentfolder/{$filename}' target='_blank'>download file here<a>";

I think you are looking for echo. So something like

echo ‘<a href=“‘.$l_filename.’”>click here</a>’;

In the line $l_filename = $stmt->fetch(); does $l_filename what is an example of what it gets set to? A complete URI, just a filename, filepath + filename, etc.?

Dani, $l_filename is the actual filname (ex: test.iso) located in the same directory as the php code

If it’s in the same directory, then you can do:

echo ‘<a href=“‘, rawurlencode($l_filename) , ’”>click here</a>’;

Thanks Dani, that worked.

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.