Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Monday, 9 January 2017

File Upload and View with PHP and MySQL

In this tutorial, We are learning that how to upload any type of file and view using PHP and MySQL.

First of all, This tutorial demonstrates that how we can upload any type of file and store in database. After uploading, we can view that file which we upload.

You can upload file like jpeg, png, gif, pdf, doc, xls, etc.

So, now let's take a look and follow the steps :

Step 1 : create database and table.

Database name -> dbtuts
Table Name -> tbl_uploads
Field Name -> file (varchar) , type (varchar), size(varchar).

Step  2 : Create a new file for database configuration.

file name : dbconfig.php


1
2
3
4
5
6
7
8
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "dbtuts";
mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server'); 
mysql_select_db($dbname) or die('database selection problem');
?>

Step 3 : Now , create a new file for file upload.

file name : index.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div style="width:100%;background-color:black;color:white;">
<label><h2>File Upload With PHP and MySql</h2></label>
</div>
<div id="body">
 <form action="upload.php" method="post" enctype="multipart/form-data">
 <input type="file" name="file" />
 <button type="submit" name="btn-upload">upload</button>
 </form>
    <br /><br />
    <?php
 if(isset($_GET['success']))
 {
  ?>
        <label>File Uploaded Successfully........<a href="view.php">click here to show the file.</a></label>
        <?php
 }
 else if(isset($_GET['fail']))
 {
  ?>
        <label>Problem While File Uploading !</label>
        <?php
 }
 else
 {
  ?>
        <label>Try to upload any files (pdf,doc,xls,jpeg,png,gif,etc...)</label>
        <?php
 }
 ?>
</div>
</body>
</html>

Step 4 : Now, write the code of uploading file in database.

file name : upload.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{         
 $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/";
 // new file size in KB
 $new_size = $file_size/1024;  
 // make file name in lower case
 $new_file_name = strtolower($file);
 $final_file=str_replace(' ','-',$new_file_name);
 if(move_uploaded_file($file_loc,$folder.$final_file))
 {
  $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
  mysql_query($sql);
  ?>
  <script>
   alert('successfully uploaded');
   window.location.href='index.php?success';
  </script>
  <?php
 }
 else
 {
  ?>
  <script>
   alert('error while uploading file');
   window.location.href='index.php?fail';
  </script>
  <?php
 }
}
?>

Step 5 : Now , write the code of view the file from database.

file name : view.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>File Upload With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div style="width:100%;background-color:black;color:white;">
<label><h2>File Upload With PHP and MySql</h2></label>
</div>
<div id="body">
 <table width="70%" border="1">
    <tr>
    <th colspan="4"><label><a href="index.php">upload new files...</a></label></th>
    </tr>
    <tr>
    <td>File Name</td>
    <td>File Type</td>
    <td>File Size(KB)</td>
    <td>View</td>
    </tr>
    <?php
 $sql="SELECT * FROM tbl_uploads";
 $result_set=mysql_query($sql);
 while($row=mysql_fetch_array($result_set))
 {
  ?>
        <tr>
        <td><?php echo $row['file'] ?></td>
        <td><?php echo $row['type'] ?></td>
        <td><?php echo $row['size'] ?></td>
        <td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td>
        </tr>
        <?php
 }
 ?>
    </table>    
</div>
</body>
</html>

There is one css file also include.

file name : style.css



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@charset "utf-8";
/* CSS Document */

*
{
 padding:0;
 margin:0;
}
body
{
 background:#fff;
 font-family:Georgia, "Gill Sans MT", Times, serif;
 text-align:center;
}
#body
{
 margin-top:100px;
}
#body table
{
 margin:0 auto;
 position:relative;
 bottom:50px;
}
table td,th
{
 padding:20px;
 border: solid #9fa8b0 1px;
 border-collapse:collapse;
}
#footer
{
 text-align:center;
 position:absolute;
 left:0;
 right:0;
 margin:0 auto;
 bottom:50px;
}

Output :



  2 comments:

  1. Casino Bonus Codes - DrmCD
    Casino Bonus Codes. 구리 출장안마 $250 광명 출장안마 Free Chip · Blackjack Bonanza. $1,000,000 · Keno 강릉 출장마사지 King. $600,000 · Slot King 화성 출장안마 · Caribbean 광명 출장안마 Hold'em. $1,000,000 · Keno King.

    ReplyDelete

Like us on Facebook

Site Visitor

Powered by Blogger.