使用php浏览文件时,写入文件的完整路径 [英] Writing the full path of the file when it is browsed using php

查看:338
本文介绍了使用php浏览文件时,写入文件的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我写了一个脚本,以便我可以浏览一个文件,看到打印的文件名称。这是脚本:

 < form action =upload.phpmethod =postenctype =multipart / form -  data> 
选择:
< input type =filename =fileToUploadid =fileToUpload>
< / form>

当我点击浏览按钮并选择一个文件,只有文件的名称打印在我的网页(我的网络浏览器是Firefox,我使用本地服务器)。有没有办法打印文件的整个地址?到目前为止,我在网上找到的大多是建议的方式,当我们提前知道/路径/到/文件。但是如果我随机选择一个文件怎么办呢?如果没有办法使用PHP,因为安全问题根据:






自定义安全级别 / p>

您也可以为Internet Explorer全局启用此行为:








Firefox





Firefox似乎不支持抓取完整的URL。但是如前所述这里似乎存在一个mozFullPath属性:

https://developer.mozilla.org/en-US/docs/Web/API/File/mozFullPath



我在我的浏览器中试过,似乎是一个不存在的属性。我找不到任何地方任何地方如何利用这个属性的任何文档。






这是一个财产, HTML5





在HTML5中,您可以写 this.files [0] 到File对象。属性包括name和lastModifiedDate,size和type,如下所示:
https://developer.mozilla.org/en-US/docs/Web/API/File



在HTML5,你实际上可以使用blob,并从所选文件创建一个对象url并显示预览。这可以通过 URL.createObjectURL(...)然后创建一个图像,并将其 src 设置为生成的临时URL。请参阅这个小提示。( credit to this post



最后,您可能会非常享受:



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


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?

解决方案

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:

Custom Security Level

You can also enable this behavior globally for Internet Explorer:


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/

这篇关于使用php浏览文件时,写入文件的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆