How to use PHP & Upload Image Gallery This the guideline/example a php scripts how to use PHP and Upload picture will be to create Image Gallery script.
ShotDev Focus:
- PHP & Upload and Create Image Gallery
Example
php_upload_gallery1.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <script language="javascript"> function fncCreateElement(){ var mySpan = document.getElementById('mySpan'); var myLine = document.getElementById('hdnLine'); myLine.value++; // Create input text var myElement1 = document.createElement('input'); myElement1.setAttribute('type',"text"); myElement1.setAttribute('name',"txtGalleryName"+myLine.value); myElement1.setAttribute('id',"txt"+myLine.value); mySpan.appendChild(myElement1); // Create input file var myElement2 = document.createElement('input'); myElement2.setAttribute('type',"file"); myElement2.setAttribute('name',"fileUpload"+myLine.value); myElement2.setAttribute('id',"fil"+myLine.value); mySpan.appendChild(myElement2); // Create <br> var myElement3 = document.createElement('<br>'); myElement3.setAttribute('id',"br"+myLine.value); mySpan.appendChild(myElement3); } function fncDeleteElement(){ var mySpan = document.getElementById('mySpan'); var myLine = document.getElementById('hdnLine'); if(myLine.value > 1 ) { // Remove input text var deleteFile = document.getElementById("txt"+myLine.value); mySpan.removeChild(deleteFile); // Remove input file var deleteFile = document.getElementById("fil"+myLine.value); mySpan.removeChild(deleteFile); // Remove <br> var deleteBr = document.getElementById("br"+myLine.value); mySpan.removeChild(deleteBr); myLine.value--; } } </script> <body> <form action="php_upload_gallery2.php" method="post" name="form1" enctype="multipart/form-data"> <input type="text" name="txtGalleryName1"><input type="file" name="fileUpload1"> <input name="btnCreate" type="button" value="+" onClick="JavaScript:fncCreateElement();"> <input name="btnDelete" type="button" value="-" onClick="JavaScript:fncDeleteElement();"><br> <span id="mySpan"></span> <input name="hdnLine" type="hidden" value="1"> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html>
php_upload_gallery2.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? mysql_connect("localhost","root","root") or die (mysql_error()); mysql_select_db("mydatabase"); for($i=1;$i<=(int)($_POST["hdnLine"]);$i++) { if($_FILES["fileUpload".$i]["name"] != "") { if(copy($_FILES["fileUpload".$i]["tmp_name"],"shotdev/".$_FILES["fileUpload".$i]["name"])) { $strSQL = "INSERT INTO gallery "; $strSQL .="(GalleryName,Picture) VALUES ('".$_POST["txtGalleryName".$i]."','".$_FILES["fileUpload".$i]["name"]."')"; mysql_query($strSQL); echo "Copy/Upload ".$_FILES["fileUpload".$i]["name"]." completed.<br>"; } } } echo "<br><a href='php_upload_gallery3.php'>View file</a>"; mysql_close(); ?> </body> </html>
php_upload_gallery3.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); $Num_Rows = mysql_num_rows($objQuery); $Per_Page = 4; // Per Page $Page = $_GET["Page"]; if(!$_GET["Page"]) { $Page=1; } $Prev_Page = $Page-1; $Next_Page = $Page+1; $Page_Start = (($Per_Page*$Page)-$Per_Page); if($Num_Rows<=$Per_Page) { $Num_Pages =1; } else if(($Num_Rows % $Per_Page)==0) { $Num_Pages =($Num_Rows/$Per_Page) ; } else { $Num_Pages =($Num_Rows/$Per_Page)+1; $Num_Pages = (int)$Num_Pages; } $strSQL .=" order by GalleryID ASC LIMIT $Page_Start , $Per_Page"; $objQuery = mysql_query($strSQL); echo"<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\"><tr>"; $intRows = 0; while($objResult = mysql_fetch_array($objQuery)) { echo "<td>"; $intRows++; ?> <center> <img src="shotdev/<?=$objResult["Picture"];?>"><br> <?=$objResult["GalleryName"];?> <br> </center> <? echo"</td>"; if(($intRows)%2==0) { echo"</tr>"; } else { echo "<td>"; } } echo"</tr></table>"; ?> <br> Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page : <? if($Prev_Page) { echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> "; } for($i=1; $i<=$Num_Pages; $i++){ if($i != $Page) { echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]"; } else { echo "<b> $i </b>"; } } if($Page!=$Num_Pages) { echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> "; } ?> </body> </html> <? mysql_close(); ?>
Create a php file and save to path root-path/myphp/
Run
http://localhost/myphp/php_upload_gallery1.php
Screenshot