php $_file['filename']['tmp'],PHP $_FILES['file']['tmp_name']:如何保存文件名和擴展名?

I am trying to upload a doc file from a form and send it to email. I am using

我正在嘗試從一個表格上載一個doc文件並將它發送到電子郵件。我用

$_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類

6 个解决方案

#1

48

$_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

帶有_file美元(“文件”)(“tmp_name”);將包含服務器上文件的臨時文件名。在處理文件之前,這只是服務器上的一個占位符

$_FILES['file']['name']; contains the original name of the uploaded file from the user's computer.

帶有_file美元(“文件”)(“名字”);包含從用戶的計算機上上傳的文件的原始名稱。

#2

10

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

$_FILES["file"]["name"] -上傳文件的名稱

從http://www.w3schools.com/php/php_file_upload.asp

#3

4

$_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"]包含您從客戶端計算機上載的文件的名稱。

#4

1

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)安全地獲取擴展。

#5

1

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"] //此名稱包含一個文件路徑,使文件讀取過程更加復雜

#6

0

Just a suggestion, but you might try the Pear Mail_Mime class instead.

只是一個建議,但是您可以嘗試Pear Mail_Mime類。

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:

否則,您可以使用一些代碼。使用rename()的Gabi Purcaru方法不能按照編寫的方式工作。參見這篇文章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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值