# Uploading Files

**URL:** https://forum.kirupa.com/t/uploading-files/58868
**Category:** programming
**Created:** [July 26, 2004, 6:56pm UTC](https://forum.kirupa.com/t/uploading-files/58868 "2004-07-26T18:56:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dreamerp](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@dreamerp](https://forum.kirupa.com/u/dreamerp)
#### Post date: [July 26, 2004, 6:56pm UTC](https://forum.kirupa.com/t/uploading-files/58868/1 "2004-07-26T18:56:37Z")

</div>

Hey,

```
 I was wondering if anyone knew a tutorial, site, or by heart how to create a form to submit a file to a database and then display that file and be able to download it. All suggestions help, thanks!
```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 26, 2004, 8:16pm UTC](https://forum.kirupa.com/t/uploading-files/58868/2 "2004-07-26T20:16:10Z")

</div>

[http://www.netspade.com/articles/php/uploading.xml](http://www.netspade.com/articles/php/uploading.xml) should work, I learned from a book called PHP By Example though. Also, you might want to try google 😉

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 26, 2004, 10:46pm UTC](https://forum.kirupa.com/t/uploading-files/58868/3 "2004-07-26T22:46:38Z")

</div>

I would like my images entered in a database or some place that I can have a command to display whatever image is placed in there.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 26, 2004, 11:07pm UTC](https://forum.kirupa.com/t/uploading-files/58868/4 "2004-07-26T23:07:03Z")

</div>

> [@dreamerp](#):
>
> Hey,
> 
> ```
> I was wondering if anyone knew a tutorial, site, or by heart how to create a form to submit a file to a database and then display that file and be able to download it. All suggestions help, thanks!
> 
> ```

Basically you would have the following:

1. A form that sends your server side page values/files to be uploaded.
2. Assuming your using PHP your code would look something like this (i got this somewhere on the forums might have changed some of it i forget)

uploadfile.php

```php

<?php

$uploaddir = 'images/'; //image directory
$uploadfile = $uploaddir . $_FILES['userfile']['name'];

while (file_exists($uploadfile))
   {
       die("File Exists, Please rename your file");
   } 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile));

$dbhost = ""; //host
$dbuser = ""; //username
$dbpwd = ""; //password
$dbname = ""; // database name
$connection = mysql_connect($dbhost, $dbuser, $dbpwd);
mysql_select_db("$dbname",$connection);
$id = $_POST[id];
$dir = $uploadfile;
$desc =$_POST[desc];

//print $id;
echo $id;
echo $dir;
echo $desc;

mysql_query("INSERT INTO file_uploader(file_id,file_directory,file_description)VALUES('$id','$dir','$desc')");
echo "File uploaded successfuly.";
?>
<br>
<?php
echo "All information has been added into the database.";
?>
<br>
<?php
echo "Thank You
";
?>
<a href="lookup.php">Click here</a> to look up all the values in items table</a>

```

That uploads the file and entering the information into a database. The code is easy and self explanitory.

1. Create a page for the user to view files (in my case images that go to flash)

viewfiles.php

```php

<html>

<head>
<title>:: Look Up Stuff ::</title></head>

<body>

<?php
// set up some variables
// server name
$server = "";
// username
$user = "";
// password
$pass = "";
// database to query
$db = ""; // make sure you change this to your database name

// open a connection to the database
$connection = mysql_connect($server, $user, $pass) or
die("Invalid server or user");

// formulate the SQL query
$query = "select * from file_uploader" or die("Error in query");

// run the query on the database
$result = mysql_db_query($db, $query, $connection) or
die("Error in query");

// display the result
while($myrow = mysql_fetch_array($result))
{   
    $id = $myrow["file_id"];
    $dir = $myrow["file_directory"];
    $de = $myrow["file_description"];
    
?>

<br>

<a href="<?php echo $dir; ?>">Your Image</a><br>

<?php
echo $de; ?><br><?php
}    
// memory flush
mysql_free_result($result);
?>

```

And thats about it

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 27, 2004, 4:19pm UTC](https://forum.kirupa.com/t/uploading-files/58868/5 "2004-07-27T16:19:04Z")

</div>

Can I have some of the sql for the table information?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 27, 2004, 6:01pm UTC](https://forum.kirupa.com/t/uploading-files/58868/6 "2004-07-27T18:01:50Z")

</div>

I dropped the database but you can see my values in my SQL statements

Table name: file\_uploader  
Fields: file\_id, file\_directory, file\_description

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 27, 2004, 6:03pm UTC](https://forum.kirupa.com/t/uploading-files/58868/7 "2004-07-27T18:03:44Z")

</div>

Not sure if digitalosophy mentioned this, but make sure that your mySQL column is blob type.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 27, 2004, 11:23pm UTC](https://forum.kirupa.com/t/uploading-files/58868/8 "2004-07-27T23:23:35Z")

</div>

Hmmm, I can’t really find the properties of the tables in the code :-/
