Hello.
Well i got bored over the holidays and thought it might of been fun to make a filehosting website like www.uppit.com

So i did, i have browsed through tutorials on the web and found one with a working upload script and basically a broken download.

Here is both (I have changed it a bit to suit my D😎.

Here is the upload (Works fine)

<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

<?php


		$dbhost = "mysql"; //Database Host
		$dbuser = "12337_upload"; //Database User
		$dbpass = "*****"; //Database Password
		$dbname = "12337_upload"; //Database Name

		//connect
		$db = mysql_pconnect($dbhost,$dbuser,$dbpass);
		mysql_select_db("$dbname",$db); 

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 
include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";

if(isset($_GET['id'])) 
{

include 'library/config.php';
include 'library/opendb.php';
$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) =                                  mysql_fetch_array($result);


echo $size;

include 'library/closedb.php'; 
exit;
}
} 
?>

And here is the download

<?php
if(isset($_GET['id'])) 
{
// if id is set then get the file with the id from database

$dbhost = "mysql"; //Database Host
			$dbuser = "12337_upload"; //Database User
			$dbpass = "****"; //Database Password
			$dbname = "12337_upload"; //Database Name

		//connect
		$db = mysql_pconnect($dbhost,$dbuser,$dbpass);
		mysql_select_db("$dbname",$db); 

include 'library/config.php';
include 'library/opendb.php';
$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) =                                  mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $size;

include 'library/closedb.php'; 
exit;
}

?>

I don't wish to use this Download script, i was more wondering if there was a way on the Upload script to Upload the file and then give a link to the download with the echo "File ** Uploaded" on the upload script.
Again similar to www.uppit.com

Thanks.

    After the the file is uploaded and inserted into the database, you can get the id using mysql_insert_id();

    http://au.php.net/manual/en/function.mysql-insert-id.php

    Something like should work (Untested)

    echo "<a href='/download.php?id=" . mysql_insert_id() . "'>Click Here</a>";

    You probably still need the download feature in a seperate file. or, you need to code some more php based on if the parameter "id" is set or not.

      Actually what you said worked fine! Thanks!
      Now i need to add a progressbar.
      Cheers!

        Write a Reply...