简体   繁体   English

如何在php中获取上传的文件路径?

[英]How can i get uploaded file path in php?

I have used this step echo $_FILES["fileField"]["tmp_name"]; 我已经使用了这一步echo $_FILES["fileField"]["tmp_name"]; but result like this. 但结果是这样。

C:\xampp\tmp\phpA9EE.tmp

How can i get exact file path? 我如何获得确切的文件路径?

An uploaded file does not have a "full path", other than temporary location where PHP has stored it during the upload process. 除了PHP在上载过程中存储文件的临时位置之外,上载的文件没有“完整路径”。

For the security of users, the browser sends only a filename of where it came from on the remote computer; 为了用户的安全,浏览器仅发送远程计算机上的文件名; for your security, you should not blindly use this (security rule of thumb: anything sent by the user is suspect and could be used to attack your system). 为了您的安全起见,请不要盲目使用此命令(安全经验法则:用户发送的任何内容都是可疑的,可用于攻击您的系统)。 You might want to filter it through a whitelist (eg remove anything other than letters and numbers) and use it as a "friendly" upload name, or you might want to ignore it completely. 您可能希望通过白名单对其进行过滤(例如,删除字母和数字以外的任何内容)并将其用作“友好的”上传名称,或者您可能希望完全忽略它。 The browser also sends a file type (eg image/jpeg ); 浏览器还发送文件类型(例如image/jpeg )。 again, this should not be trusted - the only way to know the type of a file is to use a command that looks at the content and validates it. 同样,这不应被信任 -知道文件类型的唯一方法是使用查看内容并对其进行验证的命令。

As far as PHP is concerned, what has been uploaded is a chunk of binary data; 就PHP而言,已上传的是一堆二进制数据; it saves this to a randomly named file, which is the path you have echoed there. 它将其保存到一个随机命名的文件中,该文件就是您在其中回显的路径。 The PHP manual has an introduction to how this works . PHP手册对此进行了介绍

With that path you can do one of two things: 使用该路径,您可以执行以下两项操作之一:

  • validate with is_uploaded_file() , and read the data with file_get_contents() or similar 使用is_uploaded_file()进行验证,并使用file_get_contents()或类似的方法读取数据
  • use move_uploaded_file() to put it in a permanent location of your choice 使用move_uploaded_file()将其放置在您选择的永久位置

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

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