The Illusion of "No Upload" in File Handling
When a user selects a file in a browser, the filename they see on screen is often just metadata generated by the operating system or the browser itself. It is rarely the definitive identifier for the file once it enters the server ecosystem. If your goal is to capture that specific name string from the client without performing a full binary upload of the file contents, you must understand exactly where that data lives in the HTTP request lifecycle and why attempting to store it directly in a database without validation is a dangerous pattern.
Technically, when a form submits via multipart/form-data, the browser constructs a payload that includes the filename as part of the field values sent to the server. In PHP, this information is accessible via the $_FILES superglobal array before any file stream is opened or written to disk. Specifically, the key 'name' within the associative array holds the string provided by the client. You can retrieve this value using standard array access without invoking move_uploaded_file(), fopen(), or SplFileInfo.
Consider a basic PHP implementation:
// $_FILES['myfile']['name'] contains the filename string
$filename = $_FILES['myfile']['name'];
// Store only the name in your database
$stmt = $pdo->prepare('INSERT INTO uploads (filename, created_at) VALUES (?, NOW())');
$stmt->execute([$filename]);
This approach technically satisfies the requirement of getting the filename without moving bytes over the network to a temporary disk location. However, relying solely on the client-provided string ignores the fundamental security model of file uploads. The primary pitfall here is that the $_FILES['name'] value is entirely under the control of the user. An attacker can easily manipulate this value to contain arbitrary strings, such as /etc/passwd, ../../config.php, or malicious SQL injection payloads if you are not careful with your database schema.
The critical distinction lies in what happens when you decide to store the file versus storing just the name. If you only need the filename for a reference (e.g., storing it in a database but never serving the actual file from that path), you must treat the string as untrusted data. You cannot sanitize the name by replacing slashes or spaces, because the browser might encode them differently, or the user might provide a name that looks valid but implies a different intent.
The most robust solution is to generate a unique identifier on the server side and store the original filename provided by the user in a separate column, strictly for display purposes. This decouples the database record from the file system path entirely.
// Generate a safe, unique ID for storage
$uniqueId = bin2hex(random_bytes(16));
// Capture the original client name for display
$originalName = $_FILES['myfile']['name'];
// Insert into database: store the generated ID in 'file_id' and original name in 'display_name'
$stmt = $pdo->prepare('INSERT INTO uploads (file_id, display_name, created_at) VALUES (?, ?, NOW())');
$stmt->execute([$uniqueId, $originalName]);
By following this pattern, you avoid the overhead of writing the file to disk just to read the name back out, yet you maintain strict control over how files are eventually stored and served. The trade-off is that you still receive the multipart data, which technically means the server must process the $_FILES array. If you absolutely require zero network transfer for the file content itself (e.g., bandwidth constraints), this method is insufficient because the browser has already committed to sending the data.
In scenarios where true "no upload" is mandatoryâmeaning the client should not send the binary data at allâyou must rely on server-side generation or pre-signed URLs that instruct the client to send data directly to an object storage service like S3, bypassing your PHP application's memory or disk entirely. In those architectures, the filename provided by the user is still captured as metadata during the signing process or via a separate API call before the transfer occurs.
Ultimately, capturing the filename without uploading the content is possible within standard HTTP requests because the filename travels in the headers/payload alongside the data. However, treating that string as safe to store directly without validation is a security anti-pattern. Always generate your own unique keys for file storage and keep the user-provided name purely as a label.