Published on January 26, 2024By DeveloperBreeze

// Check if a file is submitted
if (isset($_FILES['file'])) {
    // Specify the target directory
    $targetDirectory = 'uploads/';

    // Build the target file path
    $targetFile = $targetDirectory . basename($_FILES['file']['name']);

    // Get the file type
    $fileType = pathinfo($targetFile, PATHINFO_EXTENSION);

    // Check if the file type is allowed (e.g., jpg or png)
    if ($fileType == 'jpg' || $fileType == 'png') {
        // Attempt to move the uploaded file to the target directory
        if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
            echo 'File uploaded successfully!';
        } else {
            echo 'Error uploading file.';
        }
    } else {
        echo 'Invalid file type.';
    }
}

Comments

Please log in to leave a comment.

Continue Reading:

File Upload

Published on January 26, 2024

php

Various cURL Examples for API Interactions

Published on January 26, 2024

bash

Upload and Store File in Laravel

Published on January 26, 2024

php

Laravel Validation Rules for User Registration

Published on January 26, 2024

php

JavaScript File Upload using Fetch API and FormData

Published on January 26, 2024

javascript

Drag and drop Input

Published on January 26, 2024