Showing posts with label storing files in separate folders. Show all posts
Showing posts with label storing files in separate folders. Show all posts

Sunday, 25 May 2014

Storing word,excel,image,pdf file in separate folders and database using php

file.php

<table align="center">
<form method="post" action="chk.php" enctype="multipart/form-data">
<tr><td>File Type</td>
<td>
<select name="upload">
<option value="pdf">pdf
</option>

<option value="image">image
</option>
<option value="document">document
</option>
<option value="spreadsheet">spreadsheet
</option>
</select>
</td>
</tr>
<tr><td>Upload your file here</td>
<td><input type="file" name="fileload"  value="Browse"/></td></tr>
<tr><td colspan="2" align="center"><input type="submit"></td></tr>
<?php
        error_reporting(0);

if($_REQUEST["msg"])
{
echo"<tr><td colspan='2' align='center'>$_REQUEST[msg]</td></tr>";
}
?>

</form>
</table>
</div>
</div>

</body>
</html>




chk.php


<!DOCTYPE html>
<html>
<head>
<title>php</title>
</head>

<body>
<?php
include("connect.php");
$uname=$_SESSION["user"];
$extn=$_POST["upload"];
$name=$_FILES["fileload"]['name'];
$file=$_FILES["fileload"]['tmp_name'];
$ext = pathinfo($name, PATHINFO_EXTENSION);
if($extn=="spreadsheet")
{
if( $ext == "xlsx" || $ext == "xlsm" || $ext == "xls")
{

$target="Excel/".$name;
move_uploaded_file($file,$target);
mysql_query("insert into data(Name,Excelfile) values('$uname','$target') ") or die(mysql_error()."error");
header("location:file.php?v=$uname");
}
else
{
header("location:file.php?msg=Select correct type"); 

}
}
else if($extn=="document")
{
if( $ext=="txt" || $ext=="csv" || $ext=="doc" || $ext=="docx" )
{
$target="Word/".$name;

move_uploaded_file($file,$target);
header("location:file.php?v=$uname");
mysql_query("insert into data(Name,Wordfile) values('$uname','$target') ") or die(mysql_error()."error");
}
else
{
header("location:file.php?msg=Select correct type"); 

}

}
else if($extn=="image")
{
if( $ext=="jpg"||$ext=="gif"||$ext=="jpeg"||$ext=="png" )
{
$target="Images/".$name;
move_uploaded_file($file,$target);
header("location:file.php?v=$uname");
mysql_query("insert into data(Name,Image) values('$uname','$target') ") or die(mysql_error()."error");
}
else
{
header("location:file.php?msg=Select correct type"); 

}

}
else if( $extn=="pdf")
{
if( $ext=="pdf")

{
$target="Pdf/".$name;
move_uploaded_file($file,$target);
header("location:file.php?v=$uname");
mysql_query("insert into data(Name,Pdf) values('$uname','$target') ") or die(mysql_error()."error");
}
else
{
header("location:file.php?msg=Select correct type"); 

}

}
else
{
header("location:file.php?msg=Type is not supported"); 
}
?>

</body>
</html>