Professional Documents
Culture Documents
How To File Upload and View With PHP and MySQL
How To File Upload and View With PHP and MySQL
How To File Upload and View With PHP and MySQL
On12/11/2014ByPradeepKhodke
WealreadyhaveaSimpleFileUploadingtutorialinthisblogandnowThistutorialdemonstrateshowyoucan
uploadafilesusingPHPandStoreuploadedfileintotheMySQLDatabase.WithPHPit'seasytouploadanyfiles
thatyouwanttotheserver,youcanuploadMP3,Image,Videos,PDFetc...files,thiswillalsohelpyouthathow
canyoufetchuploadedfilesfromMySQLDatabaseandviewthemonBrowser,solet'stakealook.
FileUploadingIntoTheDatabase
Firstofallherei'llshowyouthathowyoucanuploadfilesusingsimplehtmlformthatsendsfiletothedatabase
throughPHPscript.
Readalso:SimpleFileUploadingScriptusingPHP
Readalso:AjaxImageUploadusingjQueryandPHP
Inthistutorialiamgoingtostorefilename,filetype,andfilesize.
importthefollowingsqlcodeinyourphpmyadmin.Databasecrediantials.
CREATEDATABASE`dbtuts`;
CREATETABLE`dbtuts`.`tbl_uploads`(
`id`INT(10)NOTNULLAUTO_INCREMENTPRIMARYKEY,
`file`VARCHAR(100)NOTNULL,
`type`VARCHAR(10)NOTNULL,
`size`INTNOTNULL
)ENGINE=MYISAM;
Databaseconfiguration.
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="dbtuts";
mysql_connect($dbhost,$dbuser,$dbpass)ordie('cannotconnecttotheserver');
mysql_select_db($dbname)ordie('databaseselectionproblem');
thehtmlform.
index.php
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FileUploadandviewWithPHPandMySql</title>
</head>
<body>
<formaction="upload.php"method="post"enctype="multipart/formdata">
<inputtype="file"name="file"/>
<buttontype="submit"name="btnupload">upload</button>
</form>
</body>
</html>
abovehtmlformsendsthedatatothefollowingPHPscriptandjoiningthishtmlandphpscriptyoucaneasily
uploadfilestothedatabase.
upload.php
<?php
if(isset($_POST['btnupload']))
{
$file=rand(1000,100000)."".$_FILES['file']['name'];
$file_loc=$_FILES['file']['tmp_name'];
$file_size=$_FILES['file']['size'];
$file_type=$_FILES['file']['type'];
$folder="uploads/";
move_uploaded_file($file_loc,$folder.$file);
$sql="INSERTINTOtbl_uploads(file,type,size)VALUES('$file','$file_type','$file_size')";
mysql_query($sql);
}
?>
DisplayFilesFromMySql.
NowwearegoingtofetchuploadedfilesfromMySqlDatabase,dataselectingfrommysqldatabaseihopeyou
knowthat...
view.php
<tablewidth="80%"border="1">
<tr>
<td>FileName</td>
<td>FileType</td>
<td>FileSize(KB)</td>
<td>View</td>
</tr>
<?php
$sql="SELECT*FROMtbl_uploads";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<tr>
<td><?phpecho$row['file']?></td>
<td><?phpecho$row['type']?></td>
<td><?phpecho$row['size']?></td>
<td><ahref="uploads/<?phpecho$row['file']?>"target="_blank">viewfile</a></td>
</tr>
<?php
}
?>
</table>
that'sit
Completescript.
dbconfig.php
<?php
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="dbtuts";
mysql_connect($dbhost,$dbuser,$dbpass)ordie('cannotconnecttotheserver');
mysql_select_db($dbname)ordie('databaseselectionproblem');
?>
index.php
FirstfilewithHtmlformwhichselectthefilefromclienttobeupload.
<?php
include_once'dbconfig.php';
?>
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FileUploadingWithPHPandMySql</title>
<linkrel="stylesheet"href="style.css"type="text/css"/>
</head>
<body>
<divid="header">
<label>FileUploadingWithPHPandMySql</label>
</div>
<divid="body">
<formaction="upload.php"method="post"enctype="multipart/formdata">
<inputtype="file"name="file"/>
<buttontype="submit"name="btnupload">upload</button>
</form>
<br/><br/>
<?php
if(isset($_GET['success']))
{
?>
<label>FileUploadedSuccessfully...<ahref="view.php">clickheretoviewfile.</a></label>
<?php
}
elseif(isset($_GET['fail']))
{
?>
<label>ProblemWhileFileUploading!</label>
<?php
}
else
{
?>
<label>Trytouploadanyfiles(PDF,DOC,EXE,VIDEO,MP3,ZIP,etc...)</label>
<?php
}
?>
</div>
<divid="footer">
<label>By<ahref="http://cleartuts.blogspot.com">cleartuts.blogspot.com</a></label>
</div>
</body>
</html>
upload.phpthisisthemainPHPScriptofthistutorialwhichuploadsthefiletotheserver.
<?php
include_once'dbconfig.php';
if(isset($_POST['btnupload']))
{
$file=rand(1000,100000)."".$_FILES['file']['name'];
$file_loc=$_FILES['file']['tmp_name'];
$file_size=$_FILES['file']['size'];
$file_type=$_FILES['file']['type'];
$folder="uploads/";
//newfilesizeinKB
$new_size=$file_size/1024;
//newfilesizeinKB
//makefilenameinlowercase
$new_file_name=strtolower($file);
//makefilenameinlowercase
$final_file=str_replace('','',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERTINTOtbl_uploads(file,type,size)VALUES('$final_file','$file_type','$new_size')";
mysql_query($sql);
?>
<script>
alert('successfullyuploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('errorwhileuploadingfile');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>
view.php
thisfileshowstheuploadedfilefromthedatabase.
<?php
include_once'dbconfig.php';
?>
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FileUploadingWithPHPandMySql</title>
<linkrel="stylesheet"href="style.css"type="text/css"/>
</head>
<body>
<divid="header">
<label>FileUploadingWithPHPandMySql</label>
</div>
<divid="body">
<tablewidth="80%"border="1">
<tr>
<thcolspan="4">youruploads...<label><ahref="index.php">uploadnewfiles...</a></label></th>
</tr>
<tr>
<td>FileName</td>
<td>FileType</td>
<td>FileSize(KB)</td>
<td>View</td>
</tr>
<?php
$sql="SELECT*FROMtbl_uploads";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<tr>
<td><?phpecho$row['file']?></td>
<td><?phpecho$row['type']?></td>
<td><?phpecho$row['size']?></td>
<td><ahref="uploads/<?phpecho$row['file']?>"target="_blank">viewfile</a></td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
style.css
andlastbutnottheleaststylesheetthatmakesbeautifyallthepages.
@charset"utf8";
/*CSSDocument*/
*
{
padding:0;
margin:0;
}
body
{
background:#fff;
fontfamily:Georgia,"TimesNewRoman",Times,serif;
textalign:center;
}
#header
{
background:#00a2d1;
width:100%;
height:50px;
color:#fff;
fontsize:36px;
fontfamily:Verdana,Geneva,sansserif;
}
#body
{
margintop:100px;
}
#bodytable
{
margin:0auto;
position:relative;
bottom:50px;
}
tabletd,th
{
padding:20px;
border:solid#9fa8b01px;
bordercollapse:collapse;
}
#footer
{
textalign:center;
position:absolute;
left:0;
right:0;
margin:0auto;
bottom:50px;
}