Nirutechnolabs

Learning & Integrating web technology and code help directory

How to use PHP & Dynamic CreateElement and Upload file

No comments
How to use PHP & Dynamic CreateElement and Upload file This the guideline/example a php scripts how using Dynamic CreateElement and Upload file
Example
php_multiple_upload1.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <script language="javascript">  
  6. function fncCreateElement(){  
  7.   
  8. var mySpan = document.getElementById('mySpan');  
  9.   
  10. var myLine = document.getElementById('hdnLine');  
  11. myLine.value++;  
  12.   
  13. // Create input text  
  14. var myElement1 = document.createElement('input');  
  15. myElement1.setAttribute('type',"text");  
  16. myElement1.setAttribute('name',"txtGalleryName"+myLine.value);  
  17. myElement1.setAttribute('id',"txt"+myLine.value);  
  18. mySpan.appendChild(myElement1);  
  19.   
  20. // Create input file  
  21. var myElement2 = document.createElement('input');  
  22. myElement2.setAttribute('type',"file");  
  23. myElement2.setAttribute('name',"fileUpload"+myLine.value);  
  24. myElement2.setAttribute('id',"fil"+myLine.value);  
  25. mySpan.appendChild(myElement2);  
  26.   
  27. // Create <br>  
  28. var myElement3 = document.createElement('<br>');  
  29. myElement3.setAttribute('id',"br"+myLine.value);  
  30. mySpan.appendChild(myElement3);  
  31. }  
  32.   
  33. function fncDeleteElement(){  
  34.   
  35. var mySpan = document.getElementById('mySpan');  
  36.   
  37. var myLine = document.getElementById('hdnLine');  
  38.   
  39. if(myLine.value > 1 )  
  40. {  
  41.   
  42. // Remove input text  
  43. var deleteFile = document.getElementById("txt"+myLine.value);  
  44. mySpan.removeChild(deleteFile);  
  45.   
  46. // Remove input file  
  47. var deleteFile = document.getElementById("fil"+myLine.value);  
  48. mySpan.removeChild(deleteFile);  
  49.   
  50. // Remove <br>  
  51. var deleteBr = document.getElementById("br"+myLine.value);  
  52. mySpan.removeChild(deleteBr);  
  53.   
  54. myLine.value--;  
  55. }  
  56. }  
  57. </script>  
  58. <body>  
  59. <form action="php_upload_gallery2.php" method="post" name="form1" enctype="multipart/form-data">  
  60. <input type="text" name="txtGalleryName1"><input type="file" name="fileUpload1">  
  61. <input name="btnCreate" type="button" value="+" onClick="JavaScript:fncCreateElement();">  
  62. <input name="btnDelete" type="button" value="-" onClick="JavaScript:fncDeleteElement();"><br>  
  63. <span id="mySpan"></span>  
  64. <input name="hdnLine" type="hidden" value="1">  
  65. <input name="btnSubmit" type="submit" value="Submit">  
  66. </form>  
  67. </body>  
  68. </html>  
php_upload_gallery2.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. mysql_connect("localhost","root","root"or die (mysql_error());  
  8. mysql_select_db("mydatabase");  
  9.   
  10. for($i=1;$i<=(int)($_POST["hdnLine"]);$i++)  
  11. {  
  12. if($_FILES["fileUpload".$i]["name"] != "")  
  13. {  
  14. if(copy($_FILES["fileUpload".$i]["tmp_name"],"shotdev/".$_FILES["fileUpload".$i]["name"]))  
  15. {  
  16. $strSQL = "INSERT INTO gallery ";  
  17. $strSQL .="(GalleryName,Picture) VALUES ('".$_POST["txtGalleryName".$i]."','".$_FILES["fileUpload".$i]["name"]."')";  
  18. mysql_query($strSQL);  
  19. echo "Copy/Upload ".$_FILES["fileUpload".$i]["name"]." completed.<br>";  
  20. }  
  21. }  
  22. }  
  23.   
  24. echo "<br><a href='php_upload_gallery3.php'>View file</a>";  
  25.   
  26. mysql_close();  
  27. ?>  
  28. </body>  
  29. </html>  
php_upload_gallery3.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. mysql_connect("localhost","root","root"or die(mysql_error());  
  8. mysql_select_db("mydatabase");  
  9. $strSQL = "SELECT * FROM gallery";  
  10. $objQuery = mysql_query($strSQL);  
  11. $Num_Rows = mysql_num_rows($objQuery);  
  12.   
  13. $Per_Page = 4;   // Per Page  
  14.   
  15. $Page = $_GET["Page"];  
  16. if(!$_GET["Page"])  
  17. {  
  18. $Page=1;  
  19. }  
  20.   
  21. $Prev_Page = $Page-1;  
  22. $Next_Page = $Page+1;  
  23.   
  24. $Page_Start = (($Per_Page*$Page)-$Per_Page);  
  25. if($Num_Rows<=$Per_Page)  
  26. {  
  27. $Num_Pages =1;  
  28. }  
  29. else if(($Num_Rows % $Per_Page)==0)  
  30. {  
  31. $Num_Pages =($Num_Rows/$Per_Page) ;  
  32. }  
  33. else  
  34. {  
  35. $Num_Pages =($Num_Rows/$Per_Page)+1;  
  36. $Num_Pages = (int)$Num_Pages;  
  37. }  
  38.   
  39. $strSQL .=" order  by GalleryID ASC LIMIT $Page_Start , $Per_Page";  
  40. $objQuery  = mysql_query($strSQL);  
  41.   
  42. echo"<table border=\"0\"  cellspacing=\"1\" cellpadding=\"1\"><tr>";  
  43. $intRows = 0;  
  44. while($objResult = mysql_fetch_array($objQuery))  
  45. {  
  46. echo "<td>";  
  47. $intRows++;  
  48. ?>  
  49. <center>  
  50. <img src="shotdev/<?=$objResult["Picture"];?>"><br>  
  51. <?=$objResult["GalleryName"];?>  
  52. <br>  
  53. </center>  
  54. <?  
  55. echo"</td>";  
  56. if(($intRows)%2==0)  
  57. {  
  58. echo"</tr>";  
  59. }  
  60. else  
  61. {  
  62. echo "<td>";  
  63. }  
  64. }  
  65. echo"</tr></table>";  
  66. ?>  
  67.   
  68. <br>  
  69. Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :  
  70. <?  
  71. if($Prev_Page)  
  72. {  
  73. echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";  
  74. }  
  75.   
  76. for($i=1; $i<=$Num_Pages$i++){  
  77. if($i != $Page)  
  78. {  
  79. echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";  
  80. }  
  81. else  
  82. {  
  83. echo "<b> $i </b>";  
  84. }  
  85. }  
  86. if($Page!=$Num_Pages)  
  87. {  
  88. echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";  
  89. }  
  90. ?>  
  91. </body>  
  92. </html>  
  93. <?  
  94. mysql_close();  
  95. ?>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP Upload Image Gallery
PHP Upload Image Gallery
PHP Upload Image Gallery

No comments :

Post a Comment