Problems with the Unique download link tutorial

Post here is you are having problems with any of the tutorials.
Post Reply
davestechuk
Posts: 26
Joined: Tue Jul 23, 2013 2:29 am

Problems with the Unique download link tutorial

Post by davestechuk »

Hi,

After taking a break for 5 minutes I came back and realized where I went wrong. Correct code below;

[syntax=php]
echo'<a href="download.php?code='.$unique_code.'">Click</a>';
[/syntax]

Thanks anyway for your help, ScTech
Last edited by davestechuk on Thu Jan 30, 2014 7:37 pm, edited 2 times in total.
ScTech
Posts: 92
Joined: Sat Aug 24, 2013 8:40 pm

Re: How to make a unique download link

Post by ScTech »

You forgot the semicolon after $code
<?php while(!$succeed = try()); ?>
davestechuk
Posts: 26
Joined: Tue Jul 23, 2013 2:29 am

Re: Problems with the Unique download link tutorial

Post by davestechuk »

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

[syntax=php]
<?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>
[/syntax]
Last edited by davestechuk on Tue Feb 04, 2014 5:29 pm, edited 1 time in total.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Problems with the Unique download link tutorial

Post by Temor »

Just change line 5 from:
[syntax=php]if(isset($_FILES['file'])){[/syntax]

to
[syntax=php]if(isset($_FILES['file']) && !empty($_FILES['file'])){[/syntax]

it should stop the upload happening if there's no file.
Post Reply