Step1: create a database called testlogin using phpmyadmin. Create a table using the following code execution through phpmyadmin query window
1 2 3 4 5 |
CREATE TABLE IF NOT EXISTS `tbl_file_storage` ( `id` int(11) NOT NULL AUTO_INCREMENT, `filename` varchar(250) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; |
step2: create a folder called ‘fileupload’ and the following files under htdocs folder
htdocs
– fileupload
step3:create a folder ‘storage’ under ‘fileupload’ folder- set permission 777 for storage folder
htdocs
– fileupload
– storage
step3: create below php files under fileupload folder
htdocs
– fileupload
– storage
-index.php
-database_connection.php
– upload.php
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 |
<html> <head> <title>Simple File upload using php</title> </head> <body> <form method="post" action="upload.php" id="form1" name="form1" enctype="multipart/form-data"> <table border="1" cellspacing="0" cellpadding="0" align="center" width="545"> <tr> <td width="1px">Choose File:</td> <td width="1px"><input type="file" name="myfiles" value=""></td> </tr> <tr> <td width="1px"></td> <td width="1px"><input type="Submit" name="Submit" value="Upload"></td> </tr> <tr> <td width="1px"></td> <td width="1px"><div style="text-color=red;"><?php echo @$_GET['msg'];?></div></td> </tr> </table> </form> </body></html> |
database_connection.php
1 2 3 4 5 6 7 8 |
<?php $connection_string = mysql_connect('localhost', 'root', ''); if (!$connection_string) { die('Could not connect: ' . mysql_error()); } mysql_select_db('testlogin', $connection_string) or die('Could not select database.'); ?> |
upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php include"database_connection.php"; if(isset($_FILES['myfiles'])){ $file_name = $_FILES['myfiles']['name']; $tmp_name =$_FILES['myfiles']['tmp_name']; //upload file to a folder called storage move_uploaded_file($tmp_name,"storage/".$file_name); //insert file name into the database $query="insert into tbl_file_storage(filename)values('$file_name')"; $query_result=mysql_query($query,$connection_string); $message="File uploaded successfully"; header("Location:index.php?msg=$message"); } ?> |