• 0

Preview image before upload in PHP code (PHP Server)


Question

This code not work on PHP Server (http://localhost)

<input type="file" name="image" onchange="document.imgPreview.src=this.value;">

more code try but also same result show me:

<script>
 function preview() {
 field = document.getElementById( 'upload' ).value;
 image = document.getElementById( 'imgPreview' );
 path = 'file://'+ field;
 path = path.replace(/\\/, '/'); // Fix Windows paths
 image.src = path;
 }
 </script>
 <input type="file" name="upload" id="upload" onchange="preview()"; />

<img src="file:///E:/Pictures/My%20Image/01.jpg" id="imgPreview" alt="Image">

why not display local computer Image

>>>That mean PHP Code not support local windows file address like

  Quote
file:///E:/Pictures/My%20Image/01.jpg

5 answers to this question

Recommended Posts

  • 0

When I copied and pasted your above code into a local .html file and tested it, it ran the way you wanted, loading the local image into the page. However, when I copied that html file across to my server and went to that page, it would run what was in onchange (evident by placing alert(this.value) in the attribute), but it wouldn't load the image. Given this, I would say that this behaviour is a common security feature in browsers.

  • 0

Careful.

IE7 won't let you display content from multiple security zones in the same browser process. It's a security feature. (And a pretty smart one, if you think about it.) And I'm sure other browsers aren't too keen on you accessing the local filesystem from javascript, either.

Find a better way to do it (Upload to a temp folder, then display it to the user, deciding whether or not to keep it.)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.