How to make a unique download link
Posted: Fri Jul 06, 2012 2:23 pm
This would be a tutorial on how to make a unique download link (download link that only works once then changes url)
The non-existent community
https://betterphp.co.uk/board/
<?php if(isset($_FILES['file'])){ $file_name = mysql_real_escape_string($_FILES['file']['name']); // To create the code we will create a charset from an array of letters and numbers and jumble it. $charset = array_flip(array_merge(range('a','z'), range('A','Z'), range(0, 9))); // The unique code is then created from taking 12 letters and numbers from the charset we just made and turn them into one string.. You can change this number to anything, but remember to change the length in the table as well. $unique_code = implode('', array_rand($charset,12)); // Insert the filename and unique code into the files table. mysql_query("INSERT INTO `files` (`file_code`, `file_name`) VALUES('{$unique_code}', '{$file_name}') "); move_uploaded_file($_FILES['file']['tmp_name'], "core/files/{$_FILES['file']['name']}"); } ?>Download part:
<?php if (isset($_GET['code'])){ $code = (int)$_GET['code']; $file = mysql_query("SELECT `file_name` FROM `files` WHERE `file_code` = '{$code}'"); if (mysql_num_rows($file) != 1){ echo 'Invalid code'; }else{ $row = mysql_fetch_assoc($file); $path = "core/files/{$row['file_name']}"; header('Content-Type: application/octetstream'); header('Content-Type: application/octet-stream'); header('Content-Description : File Transfer'); header("Content-Disposition: attachment; filename=\"{$row['file_name']}\""); header('Content-Length: ' .filesize($path)); readfile($path); // Create a new random code. $charset = array_flip(array_merge(range('a','z'), range('A','Z'), range(0, 9))); $new_code = implode('', array_rand($charset,12)); // Update the files table and change the file_code to our new code where file_code is the same as the code from the URL. // You could run a delete query here instead. mysql_query("UPDATE `files` SET `file_code` = '{$new_code}' WHERE `file_code` = '{$code}' "); } } ?>If you require further explanation, please say so