Page 1 of 1

Problems with the Unique download link tutorial

Posted: Thu Jan 30, 2014 2:26 pm
by Ehrmantraut
Hi,

After taking a break for 5 minutes I came back and realized where I went wrong. Correct code below;
echo'<a href="download.php?code='.$unique_code.'">Click</a>';
Thanks anyway for your help, ScTech

Re: How to make a unique download link

Posted: Thu Jan 30, 2014 2:55 pm
by ScTech
You forgot the semicolon after $code

Re: Problems with the Unique download link tutorial

Posted: Mon Feb 03, 2014 8:25 pm
by Ehrmantraut
Hi, I thought I had the script working correctly. However I've come across a problem, If I don't browse for a file too upload and hit upload twice the "Download file" hyperlink appears and allows me to download "download.php".

Original Tutorial Link --> http://betterphp.co.uk/board/viewtopic.php?f=9&t=1759


Index.php File
<?php

include("core/init.inc.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']}");

echo'<a href="download.php?code='.$unique_code.'">Download File</a>';
                
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="" type="text/css"/>
<title>Temporary Url</title>
</head>

<body>

<form action="" method="POST" enctype="multipart/form-data">
 
<p><input type="file" name="file" /></p>
 
<p><input type="submit" value="Upload"/></p>

</form>
</body>
</html>

Re: Problems with the Unique download link tutorial

Posted: Mon Feb 03, 2014 11:02 pm
by Temor
Just change line 5 from:
if(isset($_FILES['file'])){
to
if(isset($_FILES['file']) && !empty($_FILES['file'])){
it should stop the upload happening if there's no file.