Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. <?php
  2. include('dbcon.php');
  3. session_start();
  4. $session_id='1'; //$session id
  5. ?>
  6. <html>
  7. <head>
  8. <title>Ajax Image Upload 9lessons blog</title>
  9. </head>
  10. <script type="text/javascript" src="scripts/jquery.min.js"></script>
  11. <script type="text/javascript" src="scripts/jquery.wallform.js"></script>
  12. <script type="text/javascript" >
  13. $(document).ready(function() {
  14. $('#photoimg').die('click').live('change', function() {
  15. //$("#preview").html('');
  16. $("#imageform").ajaxForm({target: '#preview',
  17. beforeSubmit:function(){
  18. console.log('v');
  19. $("#imageloadstatus").show();
  20. $("#imageloadbutton").hide();
  21. },
  22. success:function(){
  23. console.log('z');
  24. $("#imageloadstatus").hide();
  25. $("#imageloadbutton").show();
  26. },
  27. error:function(){
  28. console.log('d');
  29. $("#imageloadstatus").hide();
  30. $("#imageloadbutton").show();
  31. } }).submit();
  32. });
  33. });
  34. </script>
  35. <style>
  36. body
  37. {
  38. font-family:arial;
  39. }
  40. .preview
  41. {
  42. width:200px;
  43. border:solid 1px #dedede;
  44. padding:10px;
  45. }
  46. #preview
  47. {
  48. color:#cc0000;
  49. font-size:12px
  50. }
  51. </style>
  52. <body>
  53. <div style="width:600px">
  54. <div id='preview'>
  55. </div>
  56. <form id="imageform" method="post" enctype="multipart/form-data" action='ajaximage.php'>
  57. Upload your image
  58. <div id='imageloadstatus' style='display:none'><img src="loader.gif" alt="Uploading...."/></div>
  59. <div id='imageloadbutton'>
  60. <input type="file" name="photoimg" id="photoimg" />
  61. </div>
  62. </form>
  63. </div>
  64. </body>
  65. </html>
  66.  
  67. <?php
  68. include('dbcon.php');
  69. session_start();
  70. $session_id='1'; //$session id
  71. $path = "uploads/";
  72. function getExtension($str)
  73. {
  74. $i = strrpos($str,".");
  75. if (!$i) { return ""; }
  76. $l = strlen($str) - $i;
  77. $ext = substr($str,$i+1,$l);
  78. return $ext;
  79. }
  80. $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
  81. if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
  82. {
  83. $name = $_FILES['photoimg']['name'];
  84. $size = $_FILES['photoimg']['size'];
  85. if(strlen($name))
  86. {
  87. $ext = getExtension($name);
  88. if(in_array($ext,$valid_formats))
  89. {
  90. if($size<(1024*1024))
  91. {
  92. $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
  93. $tmp = $_FILES['photoimg']['tmp_name'];
  94. if(move_uploaded_file($tmp, $path.$actual_image_name))
  95. {
  96. $sql="INSERT INTO user (image, uid)
  97. VALUES
  98. ('$actual_image_name','$session_id');
  99. echo "<img src='uploads/".$actual_image_name."' class='preview'>";
  100. }
  101. else
  102. echo "Fail upload folder with read access.";
  103. }
  104. else
  105. echo "Image file size max 1 MB";
  106. }
  107. else
  108. echo "Invalid file format..";
  109. }
  110. else
  111. echo "Please select image..!";
  112. exit;
  113. }
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement