Because using regular filesystem functions for this purpose might create security vulnerabilities. If you do this in your program:
rename($source, $destination);
and the attacker is able to control the value of $source
, they have gained the capability to rename (or move! -- rename
also moves files across directories) any file that your PHP process has access to.
If they can also influence $destination
or if there is some way of gaining access to the contents of the file after it is moved they can use this vulnerability to gain access to your source code at the very least, which would usually reveal authentication credentials. And it's not difficult to imagine this happening: if you accept user uploads and make them accessible over a URL this functionality would be already built into your application.
In general, it's a security issue that you have to think about; the _uploaded_file
functions are there to help you land in the pit of success.
Update (material pulled from comments):
Modern handling of file uploads (through $_FILES
) has largely made move_uploaded_file
technically unnecessary. But don't forget that:
- Technically unnecessary might still be a good idea: we are talking security, why not be extra safe?
move_uploaded_files
was introduced at a time where $_FILES
did not even exist and widespread usage of register_globals
was reality instead of a children horror story.