不能上传文件,都返回失败。经过排查发现是PHP中的is_uploaded_file函数在捣鬼,下面是具体的处理方法,有类似情况的朋友可以参考下
起因:
在一个项目中,接到用户反馈说其所有客户不能上传文件,都返回失败。经过排查发现是PHP中的is_uploaded_file函数在捣鬼。
细节分析:
在正常情况下,通过PHP 上传文件 ,需要通过is_uploaded_file函数来判断文件是否是通过 HTTP POST 上传的,这可以用来确保恶意的用户无法欺骗脚本去访问本不能访问的文件,例如 /etc/passwd。
而本次遇到的问题是本来应该是C:/WINDOWS/Temp/php99.tmp这样的tmp_name,却变成了C://WINDOWS //Temp//php99.tmp这种,导致is_uploaded_file函数返回错误的信息。
处理方式:
在加上如下代码后,问题解决。
$file['tmp_name'] = str_replace('////', ‘//', $file['tmp_name']);
注意,“////”实际字符串就是两个/,其他两个是用来表示转义的。
深入研究:
为什么在默写特定的环境下会出现这种情况呢,我们来看如下分析:
; Magic quotes for incoming GET/POST/Cookie data.
magic_quotes_gpc = On
在PHP的默认配置中magic_quotes_gpc是On的,而打开了magic_quotes_gpc参数的PHP环境会自动对GET/POST /Cookie添加addslashes效果。注意,并不会为$_FILES添加addslashes效果。
而当magic_quotes_gpc是Off的时候,由于为$_FILES数组添加了addslashes作用,反而出现了问题。也就在 magic_quotes_gpc是Off的PHP环境下都会出现此问题。
顺带说句,SVN上的MooPHP代码已经修复此问题。
is_uploaded_file函数解析:
判断文件是否是通过 HTTP POST 上传的
bool is_uploaded_file ( string $filename )
如果 filename 所给出的文件是通过 HTTP POST 上传的则返回 TRUE。这可以用来确保恶意的用户无法欺骗脚本去访问本不能访问的文件,例如 /etc/passwd。 这种检查显得格外重要,如果上传的文件有可能会造成对用户或本系统的其他用户显示其内容的话。
为了能使 is_uploaded_file() 函数正常工作,必段指定类似于 $_FILES['userfile']['tmp_name'] 的变量,而在从客户端上传的文件名 $_FILES['userfile']['name'] 不能正常运作。

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools
