290 likes | 316 Views
PHP Upload image. 1. Upload filename into database. Create 6 files and 1 folder myfile Include.php for connect PHP and database Upload1.php for input data Upload2.php for upload and insert into table Upload3.php for show all data Upload4.php for receive data for edit
E N D
1. Upload filename into database Create 6 files and 1 foldermyfile • Include.php for connect PHP and database • Upload1.php for input data • Upload2.php for upload and insert into table • Upload3.php for show all data • Upload4.php for receive data for edit • Upload5.php for update data into table
Create table files CREATE TABLE 'files' ( 'FilesID' int(4) NOT NULL auto_increment, 'Name' varchar(100) NOT NULL, 'FilesName' varchar(100) NOT NULL, PRIMARY KEY ('FilesID') ) ENGINE=MyISAM AUTO_INCREMENT=1 ;
include.php <?php mysql_connect("localhost","root","1234") or die("Error Connect to PHP Myadmin"); mysql_select_db("test") or die("Error Connect to Database"); ?>
Upload1.php <html> <body> <form name="form1" method="post" action="Upload2.php" enctype="multipart/form-data"> Name : <input type="text" name="txtName"><br> Picture : <input type="file" name="filUpload"><br> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html>
Upload2.php <html> <body> <? include ("include.php"); if(move_uploaded_file($_FILES["filUpload"]["tmp_name"], "myfile/".$_FILES["filUpload"]["name"])) { echo "Copy/Upload Complete<br>"; //*** Insert Record ***// $strSQL = "INSERT INTO files "; $strSQL .="(Name,FilesName) VALUES ('".$_POST["txtName"]."','".$_FILES["filUpload"]["name"]."')"; $objQuery = mysql_query($strSQL);
} ?> <a href="Upload3.php">View files</a> </body> </html>
Upload3.php <html> <body> <? include ("include.php"); $strSQL = "SELECT * FROM files"; $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); ?>
<table width="340" border="1"> <tr> <th width="50"> <div align="center">Files ID </div></th> <th width="150"> <div align="center">Picture</div></th> <th width="150"> <div align="center">Name</div></th> <th width="150"> <div align="center">Edit</div></th> </tr> <? while($objResult = mysql_fetch_array($objQuery)) { ?>
<tr> <td><div align="center"><?=$objResult["FilesID"];?></div></td> <td><center><img src="myfile/<?=$objResult["FilesName"];?>"></center></td> <td><center><?=$objResult["Name"];?></center></td> <td><center><a href="Upload4.php?FilesID=<?=$objResult["FilesID"];?>">Edit</a></center></td> </tr> <? } ?> </table> <? mysql_close($objConnect);?> </body> </html>
UPload4.php <html> <body> <? include ("include.php"); $strSQL = "SELECT * FROM files WHERE FilesID = '".$_GET["FilesID"]."' "; $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); $objResult = mysql_fetch_array($objQuery); ?>
<form name="form1" method="post" action="Upload5.php?FilesID=<?=$_GET["FilesID"];?>" enctype="multipart/form-data"> Edit Picture :<br> Name : <input type="text" name="txtName" value="<?=$objResult["Name"];?>"><br> <img src="myfile/<?=$objResult["FilesName"];?>"><br> Picture : <input type="file" name="filUpload"><br> <input type="hidden" name="hdnOldFile" value="<?=$objResult["FilesName"];?>"> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html>
Upload5.php <html> <body> <? //*** Update Record ***// include ("include.php"); $strSQL = "UPDATE files "; $strSQL .=" SET NAME = '".$_POST["txtName"]."' WHERE FilesID = '".$_GET["FilesID"]."' "; $objQuery = mysql_query($strSQL); if($_FILES["filUpload"]["name"] != "") {
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"myfile/".$_FILES["filUpload"]["name"])) if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"myfile/".$_FILES["filUpload"]["name"])) { //*** Delete Old File ***// @unlink("myfile/".$_POST["hdnOldFile"]); //*** Update New File ***// $strSQL = "UPDATE files "; $strSQL .=" SET FilesName = '".$_FILES["filUpload"]["name"]."' WHERE FilesID = '".$_GET["FilesID"]."' "; $objQuery = mysql_query($strSQL); echo "Copy/Upload Complete<br>"; } } ?> <a href="Upload3.php">View files</a> </body> </html>
2. Upload file into table as Blob Create 7 files • Include.php for connect PHP and database • Uploadblob1.php for input data • Uploadblob2.php for upload and insert into table • Uploadblob3.php for show all data • Uploadblob4.php for receive data for edit • Uploadblob5.php for update data into table • Viewimage for show image on the screen
include.php <?php mysql_connect("localhost","root","1234") or die("Error Connect to PHP Myadmin"); mysql_select_db("test") or die("Error Connect to Database"); ?>
Create table files2 CREATE TABLE 'files2' ( 'FilesID' int(4) NOT NULL auto_increment, 'Name' varchar(100) NOT NULL, 'FilesName' blob NOT NULL, PRIMARY KEY ('FilesID') ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Uploadblob1.php <html> <body> <form name="form1" method="post" action="Uploadblob2.php" enctype="multipart/form-data"> Name : <input type="text" name="txtName"><br> Picture : <input type="file" name="filUpload"><br> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html>
Uploadblob2.php <html> <body> <? include ("include.php"); if($_FILES["filUpload"]["name"] != "") { //*** Read file BINARY ***' $fp = fopen($_FILES["filUpload"]["tmp_name"],"r"); $ReadBinary =fread($fp,filesize($_FILES["filUpload"]["tmp_name"])); fclose($fp); $FileData = addslashes($ReadBinary);
//*** Insert Record ***// $strSQL = "INSERT INTO files2 "; $strSQL .="(Name,FilesName) VALUES ('".$_POST["txtName"]."','".$FileData."')"; $objQuery = mysql_query($strSQL); echo "Copy/Upload Complete<br>"; mysql_close(); } ?> <a href="UploadTblob3.php">View files</a> </body> </html>
Uploadblob3.php <html> <body> <? include ("include.php"); $strSQL = "SELECT * FROM files2"; $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); ?>
<table width="340" border="1"> <tr> <th width="50"> <div align="center">Files ID </div></th> <th width="150"> <div align="center">Picture</div></th> <th width="150"> <div align="center">Name</div></th> <th width="150"> <div align="center">Edit</div></th> </tr> <? while($objResult = mysql_fetch_array($objQuery)) { ?> <tr> <td><div align="center"><?=$objResult["FilesID"];?></div></td>
<td><center><img src="ViewImage.php?FilesID=<?=$objResult["FilesID"];?>"></center></td> <td><center><?=$objResult["Name"];?></center></td> <td><center><a href="Uploadblob4.php?FilesID=<?=$objResult["FilesID"];?>">Edit</a></center></td> </tr> <? } ?> </table> <? mysql_close(); ?> </body> </html>
Uploadblob4.php <html> <body> <? include ("include.php"); $strSQL = "SELECT * FROM files2 WHERE FilesID = '".$_GET["FilesID"]."' "; $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); $objResult = mysql_fetch_array($objQuery); ?>
<form name="form1" method="post" action="Uploadblob5.php?FilesID=<?=$_GET["FilesID"];?>" enctype="multipart/form-data"> Edit Picture :<br> Name : <input type="text" name="txtName" value="<?=$objResult["Name"];?>"><br> <img src="ViewImage.php?FilesID=<?=$objResult["FilesID"];?>"> <br> Picture : <input type="file" name="filUpload"><br> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> <? mysql_close(); ?> </html>
Uploadblob5.php <html> <body> <? //*** Update Record ***// include ("include.php"); $strSQL = "UPDATE files2 "; $strSQL .=" SET NAME = '".$_POST["txtName"]."' WHERE FilesID = '".$_GET["FilesID"]."' "; $objQuery = mysql_query($strSQL); if($_FILES["filUpload"]["name"] != "") {
//*** Read file BINARY ***' $fp = fopen($_FILES["filUpload"]["tmp_name"],"r"); $ReadBinary = fread($fp,filesize($_FILES["filUpload"]["tmp_name"])); fclose($fp); $FileData = addslashes($ReadBinary); //*** Update New File ***// $strSQL = "UPDATE files2 "; $strSQL .=" SET FilesName = '".$FileData."' WHERE FilesID = '".$_GET["FilesID"]."' "; $objQuery = mysql_query($strSQL); echo "Copy/Upload Complete<br>"; }
mysql_close(); ?> <a href="Uploadblob3.php">View files</a> </body> </html>
Viewimage.php <? include ("include.php"); $strSQL = "SELECT * FROM files2 WHERE FilesID = '".$_GET["FilesID"]."' "; $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); $objResult = mysql_fetch_array($objQuery); echo $objResult["FilesName"]; ?>