PHP $_FILES['file']['tmp_name']:如何保留文件名和扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3945330/
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
PHP $_FILES['file']['tmp_name']: How to preserve filename and extension?
提问by Jaspero
I am trying to upload a doc file from a form and send it to email. I am using
我正在尝试从表单上传文档文件并将其发送到电子邮件。我在用
$_FILES['file']['tmp_name'];
The problem is, it is returning a randomly generated file name. So, when it reaches the inbox, the filename is phpvwRGKN.dat (filename is random each time).
问题是,它返回一个随机生成的文件名。所以,当它到达收件箱时,文件名是 phpvwRGKN.dat(文件名每次都是随机的)。
How can I preserve the filename and extension?
如何保留文件名和扩展名?
Note: I am using geekMail class
注意:我正在使用geekMail 类
回答by Alan Geleynse
$_FILES['file']['tmp_name'];
will contain the temporary file name of the file on the server. This is just a placeholder on your server until you process the file
$_FILES['file']['tmp_name'];
将包含服务器上文件的临时文件名。这只是您服务器上的占位符,直到您处理文件
$_FILES['file']['name'];
contains the original name of the uploaded file from the user's computer.
$_FILES['file']['name'];
包含从用户计算机上传的文件的原始名称。
回答by Gabi Purcaru
$_FILES["file"]["name"] - the name of the uploaded file
$_FILES["file"]["name"] - the name of the uploaded file
回答by Bhuppi
$_FILES["file"]["tmp_name"]
contains the actual copy of your file content on the server while$_FILES["file"]["name"]
contains the name of the file which you have uploaded from the client computer.
$_FILES["file"]["tmp_name"]
包含服务器上文件内容的实际副本,同时$_FILES["file"]["name"]
包含您从客户端计算机上传的文件的名称。
回答by mellowsoon
Just a suggestion, but you might try the Pear Mail_Mime class instead.
只是一个建议,但您可以尝试使用 Pear Mail_Mime 类。
http://pear.php.net/package/Mail_Mime/docs
http://pear.php.net/package/Mail_Mime/docs
Otherwise you can use a bit of code. Gabi Purcaru method of using rename() won't work the way it's written. See this post http://us3.php.net/manual/en/function.rename.php#97347. You'll need something like this:
否则,您可以使用一些代码。Gabi Purcaru 使用 rename() 的方法不会像它写的那样工作。请参阅此帖子http://us3.php.net/manual/en/function.rename.php#97347。你需要这样的东西:
$dir = dirname($_FILES["file"]["tmp_name"]);
$destination = $dir . DIRECTORY_SEPARATOR . $_FILES["file"]["name"];
rename($_FILES["file"]["tmp_name"], $destination);
$geekMail->attach($destination);
回答by Randall Cook
Like @Gabi Purcaru mentions above, the proper way to rename and move the file is to use move_uploaded_file()
. It performs some safety checks to prevent security vulnerabilities and other exploits. You'll need to sanitize the value of $_FILES['file']['name']
if you want to use it or an extension derived from it. Use pathinfo
($_FILES['file']['name'], PATHINFO_EXTENSION)
to safely get the extension.
就像上面提到的@Gabi Purcaru 一样,重命名和移动文件的正确方法是使用move_uploaded_file()
. 它执行一些安全检查以防止安全漏洞和其他漏洞。$_FILES['file']['name']
如果要使用它或从它派生的扩展,则需要清理 的值。使用安全得到了扩展。pathinfo
($_FILES['file']['name'], PATHINFO_EXTENSION)
回答by macio.Jun
If you wanna get the uploaded file name, use $_FILES["file"]["name"]
如果您想获取上传的文件名,请使用 $_FILES["file"]["name"]
But If you wanna read the uploaded file you should use $_FILES["file"]["tmp_name"]
, because tmp_name is a temporary copy of your uploaded file and it's easier than using
但是如果你想阅读上传的文件,你应该使用$_FILES["file"]["tmp_name"]
,因为 tmp_name 是你上传的文件的临时副本,它比使用更容易
$_FILES["file"]["name"] // This name includes a file path, which makes file read process more complex
$_FILES["file"]["name"] // This name includes a file path, which makes file read process more complex