Home >Backend Development >PHP Tutorial >How to Determine if a User Has Uploaded a File in PHP?
Determining User File Upload in PHP
When implementing form validation for file uploads, handling optional uploads is crucial. To distinguish between a submitted form without an upload and a validation failure, it is necessary to verify the presence of an uploaded file.
Checking for File Upload with is_uploaded_file()
To determine whether a user has uploaded a file, you can use the is_uploaded_file() function. This function returns TRUE if a file exists with the specified filename and was uploaded via HTTP POST.
Usage:
<code class="php">if (!file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'])) { echo 'No upload'; }</code>
This code checks if a file is both present in the temporary directory ($_FILES['myfile']['tmp_name']) and was uploaded as expected. If either condition is not met, it indicates that no file was uploaded.
Additional Considerations
The above is the detailed content of How to Determine if a User Has Uploaded a File in PHP?. For more information, please follow other related articles on the PHP Chinese website!