Writing the full path of the file when it is brows

2020-04-02 18:25发布

So far I wrote a script so that I can browse for a file and see the printed name of the file. Here is the script:

 <form action="upload.php" method="post" enctype="multipart/form-  data">
 Select: 
 <input type="file" name="fileToUpload" id="fileToUpload">
 </form>

When I hit the browse button and choose a file, only the name of the file gets printed on my web page (My web-browser is Firefox and I am using a local server). Is there a way to print the whole address of the file? What I have found on the web so far were mostly suggesting ways when we know in advance "/path/to/file". But how can it be done if I randomly choose a file? If there is no way to do it with PHP because of security issues according to:

How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?,

Is it possible t do it with C, C++, html, etc?

I really need to show the local path of the directory. What are the alternatives? The answer is it can't be done? I found this website http://www.htaccesstools.com/articles/full-path-to-file-using-php/

Don't know how it works though.

The other alternative would be to define a fixed path and let the user only choose that directory and since it is known I can print it out. Does it make sense?

8条回答
够拽才男人
2楼-- · 2020-04-02 18:30

You need to change the settings of your browser and then you can access the relative path of your file.

Chrome you will get real path using this.files[0].webkitRelativePath

FirFox you will get using this.files[0].mozFullPath

查看更多
贼婆χ
3楼-- · 2020-04-02 18:36

You can do this because of security reasons. Javascript don't have permission to acces to the File System, look at this answer: How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?

查看更多
劫难
4楼-- · 2020-04-02 18:38

Older browsers used to allow unrestricted access to the full path, so it's not impossible, but due to security concerns, your best answer will be a workaround.


Internet Explorer


HTA Application

If you're working locally, one option is that you can run your page as an HTML Application. Sadly this uses Internet Explorer as the engine. But if you can get away with an HTA, this does what you want:

<!--test.hta-->
<HTML>
<HEAD>
<HTA:APPLICATION ID="testFile" BORDER="thick" BORDERSTYLE="complex"/>
<TITLE>HTA - Test file</TITLE>
</HEAD>
<BODY>
<input type="file" onchange="alert(this.value)">
</BODY>
</HTML>

Trusted Site

A much better option, is simply to use Internet Explorer and then add your page to Internet Explorer's trusted sites. Then your solution is as simply as:

<input type="file" id="fileUpload" onchange="alert(this.value)">

Here's how to add a site to your trusted sites:

step 1 step 2

Custom Security Level

You can also enable this behavior globally for Internet Explorer:

method 2


Firefox


Firefox does not appear to have support for grabbing the full URL. But as mentioned here there does seem to exist a "mozFullPath" property:
https://developer.mozilla.org/en-US/docs/Web/API/File/mozFullPath

I tried it in my browser and it seems to be a non-existent property. I cannot find any documentation anywhere regarding how to take advantage of this property. But it's a property to keep an eye out for in case it ever becomes useful.


HTML5


In HTML5, you can write this.files[0] to refer to the File object. Properties include "name" and "lastModifiedDate", "size", and "type" as mentioned here: https://developer.mozilla.org/en-US/docs/Web/API/File

In HTML5 you can actually work with blobs and create an object url from the selected file and show a preview. This can be done with URL.createObjectURL(...) then creating an image and settings its src to the resulting temporary url. See this fiddle.(credit goes to this post)

Finally, you might greatly enjoy:

https://blueimp.github.io/jQuery-File-Upload/

查看更多
欢心
5楼-- · 2020-04-02 18:48

I did hit the same issue but not in the same circumstances. Only IE10 gave me the full path while firefox (and probably all others) doesn't. In my case, i use a server with php, so i ask the user to select the file and i upload it on the server to use it how i need. I hope it helped.

查看更多
霸刀☆藐视天下
6楼-- · 2020-04-02 18:48

This seems you wanting full path of your file. Kindly try to use :__FILE__

查看更多
Root(大扎)
7楼-- · 2020-04-02 18:49

Using JavaScript and a hidden field you can do this:

$('#someHiddenField').val( $('#myFileField').val() );

but keep in mind that not all browsers return the full path (specifically FF only returns the file name).

查看更多
登录 后发表回答