简体   繁体   English

PHP $ _FILES ['file'] ['tmp_name']:如何保留文件名和扩展名?

[英]PHP $_FILES['file']['tmp_name']: How to preserve filename and extension?

I am trying to upload a doc file from a form and send it to email. 我正在尝试从表单上传doc文件并将其发送到电子邮件。 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类

$_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']['name']; contains the original name of the uploaded file from the user's computer. 包含用户计算机上传文件的原始名称。

$_FILES["file"]["name"] - the name of the uploaded file

来自http://www.w3schools.com/php/php_file_upload.asp

$_FILES["file"]["tmp_name"] contains the actual copy of your file content on the server while $_FILES["file"]["tmp_name"]包含服务器上文件内容的实际副本
$_FILES["file"]["name"] contains the name of the file which you have uploaded from the client computer. $_FILES["file"]["name"]包含您从客户端计算机上载的文件的名称。

Like @Gabi Purcaru mentions above, the proper way to rename and move the file is to use move_uploaded_file() . 就像上面提到的@Gabi Purcaru一样,重命名和移动文件的正确方法是使用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. 如果要使用它或从中派生的扩展,您需要清理$_FILES['file']['name']的值。 Use pathinfo ($_FILES['file']['name'], PATHINFO_EXTENSION) to safely get the extension. 使用pathinfo ($_FILES['file']['name'], PATHINFO_EXTENSION)安全地获取扩展名。

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

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. 使用rename()的Gabi Purcaru方法不会按照它的编写方式工作。 See this post http://us3.php.net/manual/en/function.rename.php#97347 . 请参阅此文章http://us3.php.net/manual/en/function.rename.php#97347 You'll need something like this: 你需要这样的东西:

$dir = dirname($_FILES["file"]["tmp_name"]);
$destination = $dir . DIRECTORY_SEPARATOR . $_FILES["file"]["name"];
rename($_FILES["file"]["tmp_name"], $destination);
$geekMail->attach($destination);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM