How do I place a downloadable file with a unique id for each person who downloads

Asked

Viewed 507 times

-1

How do I put a file to download to my website where the button link changes every time after the file is downloaded and never to a person to download by the same link that has already been downloaded once?

I use wordpress, I’ve seen it on websites, but do not know how to do, has how to help me?

  • So it is, your question is wide, can you put the code you have ? You have an understanding in PHP or Javascript ?

  • Even then because I don’t know much about PHP or js, I thought there was a code or plugin that we just use the file or paste a url and that’s it

  • Well I don’t know, maybe someone with more WP experience will show you 1, good luck.

  • Marcio, the site is only about programming even, plugin indication does not fit in site scope. But even if you close the issue out of scope, comments remain open if someone has a tip to give you. If you try to learn the basics to do something and have questions, I think the site will be more useful to help you.

  • 1

    In fact, I believe that it should not even be necessary an advanced level to solve your problem, maybe if you find in the code, the html related to this button and edit your question, passing more information you can save... is that it is a bit confusing what you are trying to do, at least I’m not understanding... maybe an example..

  • Using DB you get your intent!

  • DB would be the Dropbox? If it is not the

  • That’s what I’m looking for, I’ll learn php

  • @Leocaracciolo why don’t you put this as an answer ? It seems to answer the question well... So other users like similar doubt could benefit...

  • ok I will post the codes in the reply

Show 5 more comments

1 answer

3


The idea is to create a table with a unicoid field.

A page that will insert $key = uniqid(md5(Rand())); in the unicoid field and generate a download link.php? id=". $key

On the download page get the database key equal to the value of the id parameter. If it exists, force the browser to download and immediately delete the unicoid field.

downloadkey.php

$link = new mysqli ("localhost", "USUARIO", "SENHA", "DB");

if(empty($_SERVER['REQUEST_URI'])) {
    $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
}

$url = preg_replace('/\?.*$/', '', $_SERVER['REQUEST_URI']);
$folderpath = 'http://'.$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($url), '/').'/';

$key = uniqid(md5(rand()));

$time = date('U');

echo "<p>Download link: <a href=\"" . $folderpath . "download.php?id=" . $key . "\">Baixar</a></p>";
echo "<p><span class=\"box\">" . $folderpath . "download.php?id=" . $key . "</span></p>";


$sqli = ("INSERT INTO downloadkey (uniqueid,timestamp) VALUES(\"$key\",\"$time\")");
$result= mysqli_query($link,$sqli);

mysqli_close($link);

Download.php

$link = new mysqli ("localhost", "USUARIO", "SENHA", "DB");

if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}

$query = ("SELECT * FROM downloadkey Where uniqueid='".$_GET['id']."'");
$result = mysqli_query($link,$query);

if (mysqli_num_rows($result) > 0) {
    $arquivo = 'arquivo.zip';
    header('Content-type: octet/stream');
    header('Content-disposition: attachment; filename="'.$arquivo.'";'); 
    readfile($arquivo);
    $sql = ("DELETE FROM downloadkey Where uniqueid='".$_GET['id']."'");
    $result = mysqli_query($link,$sql);
    exit;
}else{
    echo "<p>Link de download que você está usando é inválido.";
    echo "<br><a href=\"downloadkey.php\">Clique aqui para obter novo link de download</a></p>";
}

mysqli_close($link);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.