Uploading Files

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!

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 :wink:

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.

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

$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


<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

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

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

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

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