Nerdegutta's logo

nerdegutta.no

PHP: upload a image from a Python program

11.07.24

Programming

This file is called from this Python program: Python: Take a picture every minute and upload it

< ? php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
        // Get details of the uploaded file
        $fileTmpPath = $_FILES['file']['tmp_name'];
        $fileName = $_FILES['file']['name'];
        $fileSize = $_FILES['file']['size'];
        $fileType = $_FILES['file']['type'];
        $fileNameCmps = explode(".", $fileName);
        $fileExtension = strtolower(end($fileNameCmps));

        // Sanitize file name
        $newFileName = md5(time() . $fileName) . '.' . $fileExtension;

        // Specify the upload directory path
        $uploadFileDir = 'uploaded_files/';
        $dest_path = $uploadFileDir . $newFileName;

        // Check if the upload directory exists, if not, create it
        if (!is_dir($uploadFileDir)) {
            mkdir($uploadFileDir, 0777, true);
        }

        // Move the file to the destination directory
        if(move_uploaded_file($fileTmpPath, $dest_path)) {
            $message ='File is successfully uploaded.';
        } else {
            $message = 'There was an error moving the uploaded file.';
        }
    } else {
        $message = 'There was some error in the file upload.';
    }
    echo $message;
} else {
    echo 'Invalid request method';
}
? >