Friday, 27 November 2015

How to Upload File in PHP

1: Create Database File.Name dbconfig.php
<!--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'); ?-->
2:Create Index File index.php Where upload box display.
<!--php include_once 'dbconfig.php'; ?--> <!--DOCTYPE html--> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>File Uploading With PHP and MySql</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="header"> <label>File Uploading With PHP and MySql</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 view 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, EXE, VIDEO, MP3, ZIP,etc...)</label> <!--php } ?--> </div> <div id="footer"> <label>By <a href="http://tahacodes.blogspot.com">tahacodes.blogspot.com</a></label> </div> </body> </html>
3:Create upload.php file where all upload configs display.Example file name,file size,file upload folder
<!--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; // new file size in KB // make file name in lower case $new_file_name = strtolower($file); // make file name in lower case $final_file=str_replace(' ','-',$new_file_name); if(move_uploaded_file($file_loc,$folder.$final_file)) { $sql="INSERT INTO tracks(sd_user_id,track_file,track_type,track_size) VALUES('".$_SESSION['id']."','$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 } } ?-->
4:Create view.php file Where all uploaded data displayed
<!--php include_once 'dbconfig.php'; ?--> <!--DOCTYPE html--> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>File Uploading With PHP and MySql</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="header"> <label>File Uploading With PHP and MySql</label> </div> <div id="body"> <table width="80%" border="1"> <tr> <th colspan="4">your uploads...<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/&lt;?php echo $row[&#39;file&#39;] ?&gt;" target="_blank">view file</a></td> </tr> <!--php } ?--> </table> </div> </body> </html>
5:Create Database name "dbtuts" and upload this file

Share

& Comment

0 comments:

Post a Comment

 

Copyright © Taha Codes™ is a registered trademark.