August 4, 2012

display image preview before it upload using PHP and JavaScript


Put this code between <head>...</head> tag.

<script type="text/javascript">
   function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
        $('#test').attr('src', e.target.result);
       }
        reader.readAsDataURL(input.files[0]);
       }
    }
</script>

Put this code in main body.

<form id="form1" runat="server">
<input onchange="readURL(this);" type="file" />
      <img alt="Image Display Here" id="test" src="#" />
  </form>

Enjoy :

52 comments:

  1. Holy crap, this is short, simple and actually works! Thanks so much for posting.

    Is there any residual data saved somewhere each time an upload takes place? Does something have to be reset, deleted, or cleared somehow?

    ReplyDelete
  2. thanks for this good one script

    ReplyDelete
  3. Thanks! This is a perfect solution!

    ReplyDelete
  4. fantastic..!!! this was very usefull...!!!! thanks a ton dude..!!!

    ReplyDelete
  5. This rily cool. does anyone know how to resize the preview image?

    ReplyDelete
  6. Thanks, this is really very useful....!!!

    ReplyDelete
  7. Thanks, really a good stuff.., but its not working with IE8 any help Appreciated..!!

    ReplyDelete
  8. Its working good in Firefox and Chrome but not in IE browser

    ReplyDelete
  9. Not display image preview, please help...

    ReplyDelete
    Replies
    1. Please write error here, I help you to solved that.

      Delete
  10. same problem Not display image preview

    ReplyDelete
    Replies
    1. Please write error here, I help you to solved that

      Delete
  11. it doesn't show jpg images :P, ithink it cant be done widout ajax after all

    ReplyDelete
  12. i upload multi file but show 1 file.u can help me , please ?

    ReplyDelete
  13. Thanks a lot. May GOD bless you!
    Pawan chouhan

    ReplyDelete
  14. Awesome dude! May God bless you :)

    ReplyDelete
  15. Picture not displaying

    ReplyDelete
    Replies
    1. Please write error here, I help you to solved that.

      Delete
  16. Thankyou for your post, but not showing image and not showing any error, only showing "alt" text

    ReplyDelete
  17. For this to work add above script basic jquery file needs for its working in script tag and or add following in script tag src attribute

    src="//code.jquery.com/jquery-1.10.2.js"

    ReplyDelete
  18. Replies
    1. @Ashish

      can you please explain exact error which generated in your local server, So I help you to solve that.

      Enjoy!

      Delete
  19. Thanks... it helped a lot.

    ReplyDelete
  20. Hi, did not work for me. Probably because the runat="server" script requires asp.net, which I don't understand what it means. I'm working with normal php files, nothing special. Help please?

    ReplyDelete
    Replies
    1. Can you try with remove runat="server" from 'form' tag?

      If any issue there after removing then tell me :)

      Delete
  21. thanks so much but how do i go about it now that am uploading multiple images at once please....waiting please

    ReplyDelete
  22. Very Helpful! Thank you!

    ReplyDelete
  23. Replies
    1. @Ness: Please send me your exact problem when upload any image? So I try to help you to solve that.

      Thanks.

      Delete
    2. i want to upload two image at same form how can i apply this logic ?

      Delete
    3. multiple images, multiple previews, id love to know too

      Delete
  24. thanks ...it works..but it doesnt if i use for multiple img,, can help?

    ReplyDelete
  25. this is great man! thanks for this/

    ReplyDelete
  26. I'm testing this on Chrome, and it doesnt work.
    All it shows it's a broken image before and after I select the image.
    I'm testing on a blank new page with the exact code showing here. Any clues?

    ReplyDelete
  27. but sir it is only one input valid
    but i use four input and different different image upload but image is not show preview

    ReplyDelete
  28. This will not work for me. It only shows the image alt text

    ReplyDelete
  29. OH! you are a life saver! thanks a lot dude

    ReplyDelete
  30. Woooh, this script worked for me, thanks a lot

    ReplyDelete

Integrating Google reCAPTCHA v3 in HTML Form with PHP

  What is Google reCAPTCHA v3? Google reCAPTCHA is a free service that helps protect websites from spam and abuse. reCAPTCHA v3 is the lates...