获取上传文件的完整路径 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5450713/
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
Getting complete PATH of uploaded file - PHP
提问by name_masked
I have a form(HTML, PHP) that lets the end user upload a file to update the database(MySQL) with the records in the uploaded file(specifically .csv). However, in the phpscript, I can only get the filename and not the complete path of the file specificed. fopen() fails due to this reason. Can anyone please let me know how I can work on finding the complete path?
我有一个表单(HTML、PHP),它允许最终用户上传文件以使用上传文件(特别是 .csv)中的记录更新数据库(MySQL)。但是,在 phpscript 中,我只能获取文件名,而不能获取指定文件的完整路径。由于这个原因, fopen() 失败。任何人都可以让我知道我如何找到完整的路径吗?
HTML Code:
HTML代码:
<html>
<head>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<p>File to upload : <input type ="file" name = "UploadFileName"></p><br />
<input type = "submit" name = "Submit" value = "Press THIS to upload">
</form>
</body>
</html>
PHP Script:
PHP脚本:
<?php
.....
......
$handle = fopen($_FILES["UploadFileName"]["name"], "r"); # fopen(test.csv) [function.fopen]: failed to open stream: No such file or directory
?>
回答by netcoder
name
refers to the filename on the client-side. To get the filename (including the full path) on the server-side, you need to use tmp_name
:
name
指客户端的文件名。要在服务器端获取文件名(包括完整路径),您需要使用tmp_name
:
$handle = fopen($_FILES["UploadFileName"]["tmp_name"], 'r');
回答by ShivarajRH
$target='uploads/'.basename($_FILES['UploadFileName']['name']);
if(move_uploaded_file($_FILES['UploadFileName']['tmp_name'],$target)) {
//Insert into your db
$fp = fopen($target, "r");
}
回答by webblover
I wrote like this:
我是这样写的:
$filePath = realpath($_FILES["file"]["tmp_name"]);
This gave me the full path to the uploaded file in PHP. If you find 0 bytes problem in file download, just modify this content-lenght line like this
这给了我在 PHP 中上传文件的完整路径。如果您在文件下载中发现 0 字节问题,只需像这样修改此 content-lenght 行
header("Content-Length: ".filesize($filePath));
Where $filePath should be absolute path to file not just file handle.
$filePath 应该是文件的绝对路径,而不仅仅是文件句柄。
回答by Vijin Paulraj
Use the following code,
使用以下代码,
$handle = fopen($_FILES["UploadFileName"]["tmp_name"], 'r');
回答by Jetro Bernardo
I use like this...
我是这样用的...
<?php
$NameOriginal = $_FILES["UploadFileName"]['name'];
$Typo_Image = $_FILES["UploadFileName"]['type'];
$name_Temp = $_FILES["UploadFileName"]['tmp_name'];
?>