1
<script type="text/javascript">
function CopyMe(oFileInput) {
var filePath = oFileInput.value;
    fh = fopen(filePath, 0);
    if (fh!=-1) {
        length = flength(fh);
        str = fread(fh, length);
        fclose(fh);
    }
document.getElementByID('myText').innerHTML = filePath;
}
</script>
<input type="file" onchange="CopyMe(this);"/>
<textarea id="myText"></textarea>

I do get any output/change in the text area! What should I do? Please help!

I used the following PHP code for that, I don't know whether it is correct:

<?php
function Read($file){
echo file_get_contents($file);
};
?>

Following was the JavaScript:

    function CopyMe(oFileInput) {
    var filePath = oFileInput.value;
    document.getElementByID('text-area3').innerHTML = "<?php Read(filePath);?>";
    }

Any suggestions?

8
  • Do you want the content of the actual file in your text-area or just the filename of the file from the input file control? Commented Dec 5, 2012 at 22:17
  • 1
    Are you confusing php with javascript? Javascript, as a clientside language does not support diskoperations such as fileopen. Commented Dec 5, 2012 at 22:18
  • this was already answered before stackoverflow.com/a/8137303/1781209 Commented Dec 5, 2012 at 22:24
  • @João Oliveira I dont wish to use AciveX, that is why I asked !!! Commented Dec 5, 2012 at 22:26
  • 1
    Your CopyMe function is run client-side, so the php code is just a string and will not be executed. you need to submit the form/file to your server to be able to print the contents using php Commented Dec 5, 2012 at 22:55

2 Answers 2

4

@apanimesh061 you have to use the FileReader api

document.getElementById('files').addEventListener('change', CopyMe, false);
function CopyMe(evt) {
    var file = evt.target.files[0];
    if (file) {
        var reader = new FileReader();
        reader.readAsDataURL(file)
    }
};

http://jsfiddle.net/wAJe4/1/

it's documented at Mozilla Developer Nework for example

Sign up to request clarification or add additional context in comments.

4 Comments

Ah! this is nice! But when I load the file it does not show its contents in the text area ....
It did not work for me, could you please recheck your jsfiddle link ?
I guess you have to replace HTML code with <input type="file" id="files" onchange="CopyMe(this);"/> <textarea id="myText"></textarea>
@JoãoOliveira That is also not working ... ! We get the path of the file but the contents are not displayed in the rext box ... what should be done ???
1

If you are running this in a browser you can't read files on the client machine using JavaScript.

1 Comment

Kindly,have a look at the PHP code that I tried, dunno whether it is the correct way ????

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.