PHP中uploaded_files函数使用方法详解_PHP
对PHP语言有些了解的朋友们都知道,它包含有功能强大的函数库。我们今天就一起来了解一下PHP uploaded_files函数的具体功能。
在早期的PHP版本中,上传文件很可能是通过如下的代码实现的:
复制代码 代码如下:
……
if (isset($_FILES['file'])) {
$tmp_name = $_FILES['file']['tmp_name'];
}
if (file_exists($tmp_name)) {
copy($tmp_name,$destfile);
}
……
但是很可能会被伪造一个$_FILES['file']数组出来,如果tmp_name的内容会被指定为/etc/passwd等敏感信息的内容,那么很容 易出现安全问题。PHP在后来的版本中用is_uploaded_file() 和 move_uploaded_file()解决了这个问题,用PHP uploaded_files函数不仅会检查$_FILES['file'] ['tmp_name']是否存在,而且会检查$_FILES['file']['tmp_name']是否是上传的文件,这样就使得伪造$_FILES 变量变得不可能,因为脚本会在检查到$_FILES['file']['tmp_name']不是PHP上传的时候终止执行。
伪造变得不可能了么?在很多的脚本里面我看到初试化部分就有@extract($_POST)之类的操作,以保证程序在register globals为off的环境下能继续运行,这样的环境下我们很轻松可以伪造$_FILES数组,甚至将原来的$_FILES数组覆盖,但是想完全的伪造 一个$_FILES数组还是很困难的,因为你无法饶过is_uploaded_file() 和 move_uploaded_file()。
但是在windows下的PHP环境下测试时,我们发现PHP的临时文件很有规律,是C:\WINDOWS \TEMP\PHP93.tmp这种格式,上传的时候文件名字会是C:\WINDOWS\TEMP\PHPXXXXXX.tmp这种格式变化,其中 XXXXXX是十六进制的数字,并且是按照顺序增加的,也就是说如果这次上传的临时文件名是C:\WINDOWS\TEMP\PHP93.tmp,那么下 次就会是C:\WINDOWS\TEMP\PHP94.tmp,临时文件名变得有规律。
但是我们可能不知道当前的文件名是什么,这可以通过PHP自身的错 误机制泄露出来,譬如我们将临时文件拷贝到一个没有权限的目录或者在目标文件里包含文件系统禁止的字符就可以将当前的临时文件名字给泄露出来,当然前提是 没有错误抑制处理。
那么到底如何饶过is_uploaded_file() 和 move_uploaded_file()呢?看看PHP uploaded_files函数部分的代码:
复制代码 代码如下:
PHP_FUNCTION(is_uploaded_file)
{
zval **path;
if (!SG(rfc1867_uploaded_files)) {
RETURN_FALSE;
}
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &path) != SUCCESS) {
ZEND_WRONG_PARAM_COUNT();
}
convert_to_string_ex(path);
if (zend_hash_exists(SG(rfc1867_uploaded_files), Z_STRVAL_PP(path), Z_STRLEN_PP(path)+1)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
它 是从当前的rfc1867_uploaded_files哈希表中查找看是否当前的文件名是否存在。其中rfc1867_uploaded_files保 存了当前PHP脚本运行过程中由系统和PHP产生的有关文件上传的变量和内容。如果存在,就说明指定的文件名的确是本次上传的,否则为否。
PHP 有个很奇怪的特性就是,当你提交一个上传表单时,PHP在做处理之前这个文件就已经被上传到临时目录下面,一直到PHP脚本运行结束的时候才会销毁掉。也 就是说,你即使向一个不接受$_FILSE变量的PHP脚本提交这样一个表单,$_FILSE变量依然会产生,文件依然会被先上传到临时目录。问题就产生 了。下面的脚本可能能说明这个问题:
复制代码 代码如下:
$a=$_FILES['attach']['tmp_name'];
echo $a.”………….”;
$file='C:\\WINDOWS\\TEMP\\PHP95.tmp';
echo $file;
if(is_uploaded_file($file)) echo ‘………………Yes';
?>
其 中C:\\WINDOWS\\TEMP\\PHP95.tmp是我猜测的临时文件名字,当时,测试这个脚本的时候我们需要向它上传一个文件或者是100个 文件,使得其中一个临时文件名为C:\\WINDOWS\\TEMP\\PHP95.tmp。如果此刻脚本有extract操作,我们就可以很方便的伪造 出一个$_FILES变量了。
不是么?可能要问伪造$_FILES变量有什么作用,我们就可以产生原来程序不允许的文件名了,PHP在处理上传的时候会对 原来的文件名有一个类似于basename()的操作,但是一旦可以伪造之后我们就可以轻易的在文件名之内加\啊../啊等等你所喜欢的任何东西
PHP uploaded_files函数的实际利用可能有点苛刻,但是也总算是PHP一点瑕疵吧,呵呵。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
