Hi. I'm currently using a form to get a file:

<form enctype="multipart/form-data" action="restore.php" method="post" name="upload" id="upload">
<input type="file" name="file"/><br/>
<input type="submit" name="Submit" value="Submit" />
</form>

But this is very unnecessary because the rest of the code is PHP and has no output to display. Is there a way to just get a javascript prompt to select the file and then pass the variable to the php as done with this?

Thanks

    I don't think there is such a possibility with JavaScript. Don't you think that it would be a little unwise if you let untrusted scripts mess with files on your disk and upload them somewhere?

      Well I just don't really want to have a page with nothing on it but an upload box

        So you want it to behave like this:
        User enters url and it automatically opens the filedialog?

        No, that is impossible to do. User must click "browse" to open the filedialog. Theres no way to click the browse button with javascript.

          The user clicks the link "Restore" (a standard hyperlink not button) then it opens a file dialogue and then runs a php script.

          It doesn't matter if this seems insecure as it's only for use locally.

            No, I meant that JavaScript won't allow you to do that, whether you think it's secure or not. People that created JavaScript thought it unwise. If you could do it, so could hackers on some malicious site.

              wilku wrote:

              People that created JavaScript thought it unwise.

              After it was exploited by malicious sites 🙂

              Any browser that's not totally oblivious to security will be like this. Any file upload will require explicit permission from the user, without any programmatic access to the permission-granting process.

                Write a Reply...