How to use PHP & Upload File To MySQL This the guideline/example a php scripts how to use PHP and Multiple Upload file and Insert to MySQL Database
ShotDev Focus:
- PHP & Upload file
- PHP & Upload file to MySQL
Example 1
php_upload1.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form action="php_upload2.php" method="post" name="form1" enctype="multipart/form-data"> <input type="text" name="txtGalleryName"><br> <input type="file" name="fileUpload"> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html>
php_upload2.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? if(copy($_FILES["fileUpload"]["tmp_name"],"shotdev/".$_FILES["fileUpload"]["name"])) { mysql_connect("localhost","root","root") or die (mysql_error()); mysql_select_db("mydatabase"); $strSQL = "INSERT INTO gallery "; $strSQL .="(GalleryName,Picture) VALUES ('".$_POST["txtGalleryName"]."','".$_FILES["fileUpload"]["name"]."')"; mysql_query($strSQL); echo "Copy/Upload completed... <a href='php_upload3.php'>View file</a>"; mysql_close(); } ?> </body> </html>
php_upload3.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? mysql_connect("localhost","root","root") or die(mysql_error()); mysql_select_db("mydatabase"); $strSQL = "SELECT * FROM gallery"; $objQuery = mysql_query($strSQL); ?> <table width="200" border="1"> <tr> <th width="200"> <div align="center">Gallery Name </div></th> </tr> <? while($objResult = mysql_fetch_array($objQuery)) { ?> <tr> <td><center><img src="shotdev/<?=$objResult["Picture"];?>"> <br><?=$objResult["GalleryName"];?></center></td> </tr> <? } ?> </table> </body> </html> <? mysql_close(); ?>
Create a php file and save to path root-path/myphp/
Run
http://localhost/myphp/php_upload1.php
Screenshot