Skip to content
PHP Logo

What resources or tutorials would you recommend for someone new to PHP looking to create a file upload feature?

To create a file upload feature in PHP, you will need to use HTML form to allow users to select and upload files, and PHP script to handle the file upload process. You can use the move_uploaded_file() function to move the uploaded file to a specific directory on the server. It is important to validate the file type and size before allowing the upload to prevent security risks.

php
```php
<?php
if(isset($_FILES['file'])){
    $file_name = $_FILES['file']['name'];
    $file_tmp = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    
    $upload_dir = 'uploads/';
    
    if(move_uploaded_file($file_tmp, $upload_dir . $file_name)){
        echo 'File uploaded successfully.';
    } else {
        echo 'Error uploading file.';
    }
}
?>
```

Make sure to create a directory named "uploads" in the same directory as your PHP script to store the uploaded files. Also, remember to set the enctype attribute of the form to "multipart/form-data" to allow file uploads.