php 使用输入类型文件选择文件夹路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1763658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:45:42  来源:igfitidea点击:

Selecting folder path using input type file

phphtml

提问by JPro

I have a problem in PHP. I am using <input type=file>to get the file name from the user. Basically I want to store the just folder path and not the file, so that when I display the path using hyperlink, the user can open the directory.

我在 PHP 中有问题。我正在使用<input type=file>从用户那里获取文件名。基本上我想存储只是文件夹路径而不是文件,这样当我使用超链接显示路径时,用户可以打开目录。

But using <input type=file>the user has to select a file then only the file textbox will get filled. Now I want to strip off the last part of the filename.

但是使用<input type=file>用户必须选择一个文件,然后只有文件文本框会被填充。现在我想去掉文件名的最后一部分。

How do I do this ? Or is there a better way to solve this problem?

我该怎么做呢 ?或者有没有更好的方法来解决这个问题?

EDIT:

编辑:

I am using something like this in say a file named 1.html:

我在一个名为 1.html 的文件中使用了这样的东西:

<form method="post" action="update.php">
    <input type="file" name="attachment" id="attachment"></td> </tr>
 </form>

Now, in update.php I am using something like this:

现在,在 update.php 我使用的是这样的:

$logpath1=$_POST['attachment'];

Basically I am getting the file name in $logpath1 that user selects. But the thing is input type = file will only work for files and not folders.

基本上我在 $logpath1 中获取用户选择的文件名。但问题是 input type = file 仅适用于文件而不适用于文件夹。

So, how do I strip the filename from the path?

那么,如何从路径中删除文件名?

Example: if user selects,

示例:如果用户选择,

C:\Documents and Settings\myusee\Desktop\new 2.cpp

I want to strip off new 2.cpp.

我想脱光new 2.cpp

I am not sure how can I use explode. Moreover I am running on XAMPP Lite.

我不确定如何使用爆炸。此外,我在XAMPP Lite上运行。

回答by dfilkovi

You can do this another way, put a hidden field <input type="hidden" id="something" name="something" />and use jQuery (JavaScript) to select the value of the file input box and put it into hidden field before submission and after submission you will have a whole path, I don't know if it works but it's worth a try.

你可以换一种方式,把一个隐藏字段<input type="hidden" id="something" name="something" />,使用jQuery(JavaScript)来选择文件输入框的值,提交前把它放到隐藏字段中,提交后你会得到一个完整的路径,我不知道它有效,但值得一试。

回答by Talvi Watia

1) ANYinformation about the clients file structure directory path is a security risk. POST submits, JavaScript, Flash, Java applications or any other web-based method that has the ability to do this is an exploit. I would doubt Firefox, Internet Explorer, Safari, Opera, or any other browser is going to allow you to do this. If a method is found - a later version most likely will patch this exploit and your code will be rendered useless.

1)任何有关客户端文件结构目录路径的信息都存在安全风险。POST 提交、JavaScript、Flash、Java 应用程序或任何其他能够执行此操作的基于 Web 的方法都是一种漏洞利用。我怀疑 Firefox、Internet Explorer、Safari、Opera 或任何其他浏览器是否会允许您执行此操作。如果找到方法 - 较新的版本很可能会修补此漏洞,并且您的代码将变得无用。

2) If you want to instead display a directory of your server directory, you can use this PHP code to get the full path to the temp directory where the uploaded file is stored.

2) 如果您想改为显示服务器目录的目录,您可以使用此 PHP 代码获取存储上传文件的临时目录的完整路径。

$temp_path=$_FILES['attachment']['tmp_name'];

However, this would be a security risk for your SERVER. Also, the temp path is normally denied access from the web. Normally, only the PHP interpreter can access this.

但是,这对您的SERVER来说是一个安全风险。此外,临时路径通常会被拒绝从 Web 访问。通常,只有 PHP 解释器可以访问它。

Usually, once a file is uploaded, if it is valid, it can be moved this way:

通常,一旦文件上传,如果它有效,可以通过以下方式移动:

move_uploaded_file($_FILES['attachment']['tmp_name'], $the_directory_i_want);

But since $the_directory_i_wantis a variable youset, you would already know this.

但是由于$the_directory_i_want设置的变量,您应该已经知道这一点。

It sort of sounds like you are trying to use WAMPand PHP as a local file utility of some sort. If that is the case, PHP may not be a good solution. You may instead want to look at creating an .HTA application.

听起来您好像在尝试将WAMP和 PHP 用作某种本地文件实用程序。如果是这种情况,PHP 可能不是一个好的解决方案。您可能想要查看创建一个.HTA application.

回答by Pekka

Most browsers suppress accessing the path info (or any other part of a file input box) for security reasons. With normal file inputs, this can not be done. Maybeone of the Flash-based uploading plugins like swfbox allow presetting a directory.

出于安全原因,大多数浏览器禁止访问路径信息(或文件输入框的任何其他部分)。对于普通的文件输入,这是无法做到的。也许像 swfbox 这样的基于 Flash 的上传插件之一允许预设目录。