DeveloperBreeze

File Upload with Type Validation in PHP

php
// 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.';
    }
}

Continue Reading

Discover more amazing content handpicked just for you

Code
javascript

JavaScript File Upload using Fetch API and FormData

No preview available for this content.

Jan 26, 2024
Read More
Code
php

Laravel Validation Rules for User Registration

public function rules()
{
    return [
        'name' => 'required|string|max:255',
        'email' => 'required|email|unique:users,email',
    ];
}

Jan 26, 2024
Read More
Code
php

Upload and Store File in Laravel

// Get the uploaded file from the request
$file = $request->file('file');

// Store the file in the 'uploads' directory
$filename = $file->store('uploads');

Jan 26, 2024
Read More
Code
bash

Various cURL Examples for API Interactions

No preview available for this content.

Jan 26, 2024
Read More
Code
php

PHP File Upload

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!