Distinguishing name and tmp_name in PHP File Uploads
When handling file uploads in PHP, the $_FILES superglobal is your primary source of truth regarding an incoming resource. However, confusing the contents of ['name'] and ['tmp_name'] can lead to logic errors that compromise security or functionality. Specifically, failing to distinguish between these two values often results in attempting to move a file from one location to itself, effectively doing nothing, or worse, creating a duplicate copy before cleanup occurs.
The distinction is fundamental: $_FILES['file']['name'] represents the original filename provided by the client. This string includes the full path and extension exactly as it existed on the user's device. If a user selects photo.jpg from their desktop, that is precisely what resides in the name index. PHP does not sanitize or alter this value during the initial reception phase; it reflects the sender's intent.
In contrast, $_FILES['file']['tmp_name'] contains the temporary filesystem path where PHP has moved the uploaded data for storage. This is a server-side construct, typically located within the system's temporary directory (e.g., /tmp/php_upload_12345 on Linux or C:\Windows\Temp\php_upload_xyz on Windows). The key takeaway here is that this path is generated by the operating system and the PHP runtime, not the user. It serves as a staging area for the file before you decide what to do with it permanently.
Understanding this separation dictates how you must handle the file lifecycle. A common architectural mistake involves attempting to rename the file using the original name directly onto the server's temp directory without moving it first. For instance, if your logic assumes rename($_FILES['file']['tmp_name'], $_FILES['file']['name']) will work, it will fail because both arguments point to different filesystem locations with conflicting intents. The source file sits in the temp folder, while the destination name implies a specific path relative to your web root. You must explicitly move the file from the temporary location to your target directory using the original filename as the new name, or generate a new unique server-side filename entirely.
Security implications arise directly from this distinction. If you blindly trust $_FILES['file']['name'] for renaming purposes without validation, you open yourself up to path traversal attacks or arbitrary file overwrites. A malicious actor could upload a file named ../../etc/passwd.jpg. While the temp name is safe (it's random), using the unvalidated original name as the destination can overwrite critical system files if your directory structure isn't strictly controlled.
The correct implementation pattern involves separating concerns: always move the file from tmp_name to a secure, application-specific directory first, then rename it within that directory to a sanitized version of the name. This ensures the file never exists in its original, potentially dangerous form on your server's public web root.
For developers using modern PHP frameworks like Laravel, this manual handling is abstracted away. The framework automatically manages the temporary storage and provides methods like move() or store() that handle the transition from tmp_name to a persistent location safely. If you are building raw handlers outside of such environments, always treat tmp_name as the source of truth for file movement operations, while treating name strictly as metadata to be validated or sanitized before assignment.