Skip to content
PHP Logo

How can a file uploaded through a form be accessed and transferred using PHP?

To access and transfer a file uploaded through a form using PHP, you can use the $_FILES superglobal array to get information about the uploaded file, move the file to a desired location on the server using the move_uploaded_file() function, and then perform any necessary actions on the file such as saving its details to a database or processing its contents.

php
<?php
if(isset($_FILES['file'])){
    $file_name = $_FILES['file']['name'];
    $file_tmp = $_FILES['file']['tmp_name'];
    $file_destination = 'uploads/' . $file_name;
    
    if(move_uploaded_file($file_tmp, $file_destination)){
        echo 'File uploaded successfully!';
        
        // Additional actions such as saving file details to a database or processing file contents can be performed here
    } else {
        echo 'Failed to upload file.';
    }
}
?>