Image Upload

Language

Hello guys, In this code you will learn on how to upload image using PHP/MySQL. Import the image_upload.sql into MySQL. Hope that you will like this code. If you have question about this code, email me at [email protected]. Thank you. Sample Code:
  1. <?php
  2. include('header.php');
  3. ?>
  4. <body>
  5. <br>
  6. <br>
  7. <div class="container">
  8. <div class="row-fluid">
  9. <div class="span12">
  10. <form class="form-horizontal" method="POST" enctype="multipart/form-data">
  11. <div class="control-group">
  12. <label class="control-label" for="input01">Image:</label>
  13. <div class="controls">
  14. <input type="file" name="image" class="font" required>
  15. </div>
  16. </div>
  17. <div class="control-group">
  18. <div class="controls">
  19. <button type="submit" name="submit" class="btn btn-success">Upload</button>
  20. </div>
  21. </div>
  22. </form>
  23. </div>
  24. <div class="span12">
  25. <ul class="thumbnails">
  26. <?php
  27. $query=mysql_query("select * from image")or die(mysql_error());
  28. while($row=mysql_fetch_array($query)){
  29. ?>
  30. <li class="span2">
  31. <div class="thumbnail"> <img src="<?php echo $row['location']; ?>" width="100" height="100" alt="" class="img-rounded"> </div>
  32. </li>
  33. <?php
  34. }
  35. ?>
  36. </ul>
  37. </div>
  38. </div>
  39. <?php
  40. if (isset($_POST['submit'])) {
  41.  
  42. $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
  43. $image_name = addslashes($_FILES['image']['name']);
  44. $image_size = getimagesize($_FILES['image']['tmp_name']);
  45.  
  46. move_uploaded_file($_FILES["image"]["tmp_name"], "upload/" . $_FILES["image"]["name"]);
  47. $location = "upload/" . $_FILES["image"]["name"];
  48.  
  49. mysql_query("insert into image (location)
  50. values ('$location')
  51. ") or die(mysql_error());
  52. header('location:index.php');
  53. }
  54. ?>
  55. </div>
  56. </body>
  57. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted byAnonymous (not verified)on Sun, 05/19/2013 - 01:02

How much size image can be uploaded? I try upload image size greater than 1000KB. Its not work.

Add new comment