Skip to main content
irrelevant tags removed
Source Link
Billal Begueradj
  • 3.4k
  • 10
  • 25
  • 34

I would appreciate a review of the PHP for handling the $_POST request. I tried to make it as simple as possible, but I'm not sure if there are any major security concerns I'm overlooking. Thanks!

I would appreciate a review of the PHP for handling the $_POST request. I tried to make it as simple as possible, but I'm not sure if there are any major security concerns I'm overlooking. Thanks!

I would appreciate a review of the PHP for handling the $_POST request. I tried to make it as simple as possible, but I'm not sure if there are any major security concerns I'm overlooking.

Source Link

Uploading multiple files to server with PHP and JavaScript

I wrote some code to upload multiple files to a server with PHP and JavaScript. I'm happy with the form and JavaScript, below.

<form method="post" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple>
    <input type="submit" value="Upload File" name="submit">
</form>

<script>
    const url = 'process.php';
    const submit = document.querySelector('[type=submit]');

    submit.addEventListener('click', e => {
        e.preventDefault();

        const files = document.querySelector('[type=file]').files;
        const formData = new FormData();

        for (let i = 0; i < files.length; i++) {
            let file = files[i];

            formData.append('files[]', file);
        }

        fetch(url, {
            method: 'POST',
            body: formData
        }).then(response => {
            console.log(response);
        });
    });
</script>

I would appreciate a review of the PHP for handling the $_POST request. I tried to make it as simple as possible, but I'm not sure if there are any major security concerns I'm overlooking. Thanks!

<?php 
if ($_SERVER["REQUEST_METHOD"] === 'POST') {
    if (isset($_FILES['files'])) {
        $errors = [];
        $path = 'uploads/';
        $extensions = ['jpg', 'jpeg', 'png', 'gif', 'html', 'txt', 'pdf', 'doc'];

        $allFiles = count($_FILES['files']['tmp_name']);
        
        for ($i = 0; $i < $allFiles; $i++) {  
            try {
                $file_name = $_FILES['files']['name'][$i];
                $file_tmp = $_FILES['files']['tmp_name'][$i];
                $file_type = $_FILES['files']['type'][$i];
                $file_ext = strtolower(end(explode('.', $_FILES['files']['name'][$i])));

                $file = $path . $file_name;
                
                if (!in_array($file_ext, $extensions)) {
                    throw new Exception('Extension not allowed: ' . $file_name . ' ' . $file_type . "\n");
                }

                if ($file_size > 2097152) {
                    // 2MB
                    throw new Exception('File size exceeds limit: ' . $file_name . ' ' . $file_type. "\n");
                }
                move_uploaded_file($file_tmp, $file);
            } catch (Exception $e) {
                error_log($e->getMessage(), 3, '/path/to/error_log');
            }
        }
    }
}