php mysql upload audio - Web Development and Web Design Codes

Latest

Friday, May 26, 2017

php mysql upload audio

PHP MySQL Upload ,Save and Download Audio


In This Lesson we will Learn how to Upload and Download Audio using PHP and MySQL.
This is Very simple and Basic Lesson for Beginners.
How to Upload large file in php
##What You will Need:
##Create create 4 files and 1 folder named 'files' in same folder.
1.connect.php
2.index.php
3.upload.php
4.style.css
5. files [folder]
##Image
Database:
Create Database Named: test
Create Table named: audio
Using The Below SQL Commands for Creating Table 'audio'
 CREATE TABLE IF NOT EXISTS `audio` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `size` varchar(100) NOT NULL,
  `type` varchar(10) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
 

Now copy The Below Codes for Certain File.
connect.php:
 <?php
$host="localhost";
$username="root";
$password="";
$dbname="test";
mysql_connect("$host","$username","$password");
mysql_select_db("$dbname");
?>
 

index.php:
Create Index page to Display Uploaded Audio with Download Button .
 <?php
include("connect.php");//Include our Connect file
include('upload.php'); //Include our Upload file
?>
<hr/>
<link rel="stylesheet" href="style.css" type="text/css"/>
<table>
 <tr>
   <th>Audio Name</th>
   <th>Size</th>
   <th>Type</th>
   <th>Action</th>
  </tr>
 <?php
 $sql="SELECT*FROM audio";
 $result=mysql_query($sql);
 while($row=mysql_fetch_array($result)){?>
  <tr>
  <td><?php echo $row['name'];?></td>
  <td><?php echo $row['size']/(1024*1024);?>MB</td>
  <td><?php echo $row['type'];?></td>
  <td><a href="files/<?php echo $row['name'];?>">Download</a></td>
  </tr>
  <?php }?>
  </table>
 

upload.php:
Create Upload File to Display and Process Upload Select Audio.We will Upload and Save Audio in OUr Database
Uisng This Upload File.
 <h2>Upload,Save and Download Audio Using PHP MySQL</h2>
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="audio"/>
<input type="submit" name="submit" value="Upload"/>
</form>
<?php
include("connect.php");
if(isset($_POST['submit'])){
 //Targeting Folder
 $target="files/";
 $target=$target.basename($_FILES['audio']['name']);
 //Getting Selected Audio Type
 $type=pathinfo($target,PATHINFO_EXTENSION);
 //Allow Certain File Format To Upload
 if($type!='mp3' && $type!='amr' && $type!='wav' ){
  echo "Only Mp3,AMR,WAV file format are allowed to Upload";
 }else{
  //checking for Exsisting Audio Files
  if(file_exists($target)){
   echo "File Exist";
  }else{
   
   //Moving The Audio file to Desired Directory
  $uplaod_success=move_uploaded_file($_FILES['audio']['tmp_name'],$target);
  if($uplaod_success==TRUE){
   //Getting Selected Audio Information
   $name=$_FILES['audio']['name'];
      $size=$_FILES['audio']['size'];
   $sql="INSERT INTO audio (name,size,type)VALUES('$name','$size','$type')";
   $result=mysql_query($sql);
   if($result==TRUE){
    echo "Your Audio '$name' Successfully Upload and Information Saved Our Database||<a href=index.php>View Upload file</a>";
   }
  }
  }
  

 }
 
}
?>
 

style.css:
Create some CSS Codes to Design Our Index Page.
table th, td{
   border: 1px solid green;
   text-align:left;
}
th,td{
 padding:8px;
 
}
table{
 border-collapse:collapse;
 width:50%;
}
table td a{
 background-color: #4CAF50;
    border: none;
    color: white;
    padding: 7px 12px;
    text-decoration: none;
    margin: 4px 2px;
    cursor: pointer;
 
}
th{
 padding:15px;
 background:lightgreen;
 font-size:20px;
 font-weight:bold;
}

Now Upload audio and Get result.Thank You for Reading Our Post. Visit Again for More Codes.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.