No posts.
No posts.
♫ UPLOAD FILE IN DB:


first of all create a new directory named "upload"
then, create a database and table with this code (here db creation code is not present as i created db manualy with db creation code):



<?php

$dbc=mysql_connect("localhost","root","") or die("error in server conn.");

$dbname="databasename2";

$dbs=mysql_select_db($dbname,$dbc) or die("error in db selection");

$sql="CREATE TABLE table1 (

id int(10) not null auto_increment,

name varchar(55) not null,

type varchar(55) not null,

size int(55) null,

path longblob,

primary key (id)

)";

mysql_query($sql) or die(mysql_error());

mysql_close($dbc);

?>



after that,
write the following php code and save it as upload.php



<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="16777216">

vinput 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

$dbname="databasename2";

$dbc=mysql_connect("localhost","root","") or die("error in db connection");

$dbs=mysql_select_db($dbname,$dbc) or die("error in db selection");
$uploadDir = "upload/";

if(isset($_POST['upload']))

{

$fileName = $_FILES['userfile']['name'];

$tmpName = $_FILES['userfile']['tmp_name'];

$fileSize = $_FILES['userfile']['size'];

$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);

if (!$result) {
echo "Error uploading file";

exit;

}

if(!get_magic_quotes_gpc())

{

$fileName = addslashes($fileName);

$filePath = addslashes($filePath);

}

$query = "INSERT INTO table1 (name, size, type, path ) ".

"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

echo "<br>Files successfully uploaded<br>";

}

?>

♫ DOWNLOAD FILE FROM DB

♫ DOWNLOAD FILE FROM DB:


we need two php file (a) file_download.php and (b) file_download2.php (make sure you have 'upload' directory from where the files would be downloaded)


(a) file_download.php


<?php

$dbh = mysql_connect("localhost","root","") or die("There was a problem with the database connection.");

$dbs = mysql_select_db("databasename2", $dbh) or die("There was a problem selecting the categories.");

$result=mysql_query("SELECT * FROM table1 ");

echo "id \n\n";

echo "name \n\n";

echo "size(byte)\n\n";

echo "<br><br>";

while($row=mysql_fetch_array($result))

{

echo "$row[id]"."\n\n\n\n";

echo "$row[name]"."\n\n\n\n";

echo "$row[size]";

echo " <td>" . stripslashes($row->description) . "</td>\n";

echo " <td>( <a href='file_download2.php?id=$row[id]'>Download</a> )</td>\n";

echo " </tr>";

echo "<br><br>";

}

mysql_free_result($result);

?>


(b) file_download2.php


<?php

$dbc=mysql_connect("localhost","root","") or die("error in db connection");

$dbs=mysql_select_db("databasename2",$dbc) or die("error in db selection");

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

{

$id = $_GET['id'];

$query = "SELECT name, type, size, path FROM table1 WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');

list($name, $type, $size, $filePath) = mysql_fetch_array($result);

header("Content-length: $size");

header("Content-type: $type");

header("Content-Disposition: attachment; filename=$name");

header("Content-Description: PHP Generated Data");

readfile($filePath);

exit;

}

?>


( note: i was not able to download files with associated format with the above code,
after download, rename the file with its fomat, like- after download,
the audio file would be 'strings' and you have to rename it as 'strings.mp3', thats all )