Friday, November 25, 2011

♫ 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">&lt;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 "&lt;br>Files successfully uploaded&lt;br>";

}

?>

No comments:

Post a Comment