在 php/javascript 中打开/浏览对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14071120/
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
Open/Browse Dialog box in php/javascript?
提问by Dhwani
I am working on a PHP project, in which I need to store a path of an image when user select an image from open dialog box from a specified directory. How can I do this? I don't know how to open the Open/Browse dialog box and how to get that path in PHP/javascript. And I want that my other form data don't flush when I open the Open/Browse Dialog.(I want to put image file's path that user has selected in my database, so I can reduce my database size.)
我正在处理一个 PHP 项目,当用户从指定目录的打开对话框中选择图像时,我需要在其中存储图像的路径。我怎样才能做到这一点?我不知道如何打开“打开/浏览”对话框以及如何在 PHP/javascript 中获取该路径。而且我希望我的其他表单数据在打开打开/浏览对话框时不会刷新。(我想将用户选择的图像文件的路径放在我的数据库中,这样我就可以减少我的数据库大小。)
采纳答案by Sinan
You can put a form element by using <input type="file">
您可以使用以下方式放置表单元素 <input type="file">
If you only want the path without uploading the file. You can use javascript.
如果您只想要路径而不上传文件。您可以使用 JavaScript。
If you post the data to the server file's info will be available to PHP but also the file will be sent to server as well.
如果您将数据发布到服务器文件的信息将可用于 PHP,但该文件也将发送到服务器。
Check the Javascript File Api examples here if you want more .. http://www.html5rocks.com/en/tutorials/file/dndfiles/
如果您想要更多,请在此处查看 Javascript File Api 示例.. http://www.html5rocks.com/en/tutorials/file/dndfiles/
回答by Stefan Dunn
You can use file uploading forms with html and send the form to your PHP file to handle the file contents. When a file is sent to the server it is stored in a temporary location.
您可以使用带有 html 的文件上传表单并将表单发送到您的 PHP 文件来处理文件内容。当文件被发送到服务器时,它被存储在一个临时位置。
W3Schools has a good tutorial on this, the HTML becomes:
W3Schools 有一个很好的教程,HTML 变为:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
and the PHP:
和 PHP:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
回答by Vladimir Gordienko
<input type="file">
no? or i'm something missing?
不?或者我缺少什么?
回答by toonice
For dotNetAddict (if they are still interested) and any others similarly interested, try the following link for a good explanation of how to obtain the path to a file....
对于 dotNetAddict(如果他们仍然感兴趣)和任何其他类似感兴趣的人,请尝试以下链接以获得有关如何获取文件路径的良好解释....

