From 0960ef32196825bc3e2a88c176d19a84bf9536e1 Mon Sep 17 00:00:00 2001 From: benninghoven Date: Mon, 29 Apr 2024 05:27:55 -0700 Subject: [PATCH] added php database functionality to upload images --- backend/docker-compose.yml | 41 ++++++++++++ backend/php/index.php | 66 +++++++++++++++++++ database/docker-compose.yml | 21 ------ database/init.sql | 11 ---- .../connect.php | 23 ------- .../index.html | 30 --------- 6 files changed, 107 insertions(+), 85 deletions(-) create mode 100644 backend/docker-compose.yml create mode 100755 backend/php/index.php delete mode 100644 database/docker-compose.yml delete mode 100644 database/init.sql delete mode 100644 testing/database_frontend_communication/connect.php delete mode 100644 testing/database_frontend_communication/index.html diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml new file mode 100644 index 0000000..868f25a --- /dev/null +++ b/backend/docker-compose.yml @@ -0,0 +1,41 @@ +version: '3.8' + +services: + + php-runtime: + image: php:latest + ports: + - "8000:8000" + working_dir: /var/www/html + command: > + sh -c "docker-php-ext-install mysqli pdo pdo_mysql && php -S 0.0.0.0:8000" + volumes: + - ./php:/var/www/html + networks: + static-network: + + database: + container_name: database + image: mysql:8 + command: --default-authentication-plugin=mysql_native_password + environment: + MYSQL_ROOT_PASSWORD: root + ports: + - "3306:3306" + networks: + static-network: + ipv4_address: 172.20.0.5 + + adminer: + container_name: adminer + image: adminer + ports: + - 8080:8080 + networks: + static-network: + +networks: + static-network: + ipam: + config: + - subnet: 172.20.0.0/16 diff --git a/backend/php/index.php b/backend/php/index.php new file mode 100755 index 0000000..68cf975 --- /dev/null +++ b/backend/php/index.php @@ -0,0 +1,66 @@ + + + + Upload Image + + + + +
" method="post" enctype="multipart/form-data"> + Select image to upload: + + +
+ +connect_error) { + die("Connection failed: " . $mysqli->connect_error); + } + + + $sql = "CREATE DATABASE IF NOT EXISTS image_database"; + $mysqli->query($sql); + + $sql = "CREATE TABLE IF NOT EXISTS image_database.image_table ( + id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, + image_name VARCHAR(255) NOT NULL, + image_file LONGBLOB NOT NULL + )"; + $mysqli->query($sql); + + + + if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_FILES['image']['tmp_name'])) { + + $image_name = $_FILES['image']['name']; + $image = $_FILES['image']['tmp_name']; + $imgContent = addslashes(file_get_contents($image)); + + $sql = "INSERT INTO image_database.image_table (image_name, image_file) VALUES ('$image_name', '$imgContent')"; + + // Devin: NEED TO REDIRECT USER, BECAUSE IF PAGE IS REFRESHED IT WILL UPLOAD TO DATABASE AGAIN + // There probably is a lot better solution, like clearing the form on refresh? Not sure. What do I know? I just + // use chatGPT and don't know anything about web development. + if ($mysqli->query($sql) === TRUE) { + echo "Uploaded $image_name successfully."; + } else { + echo "Error uploading image: " . $mysqli->error; + } + } + +?> + + + + + + + + diff --git a/database/docker-compose.yml b/database/docker-compose.yml deleted file mode 100644 index 8bda106..0000000 --- a/database/docker-compose.yml +++ /dev/null @@ -1,21 +0,0 @@ -version: '3.8' - -services: - database: - container_name: database - image: mysql:8 - command: --default-authentication-plugin=mysql_native_password - restart: always - environment: - MYSQL_ROOT_PASSWORD: password - ports: - - "3306:3306" - volumes: - - ./init.sql:/docker-entrypoint-initdb.d/init.sql - - adminer: - container_name: adminer - image: adminer - restart: always - ports: - - 8080:8080 diff --git a/database/init.sql b/database/init.sql deleted file mode 100644 index 4c25fc9..0000000 --- a/database/init.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE DATABASE IF NOT EXISTS image_database; - -USE image_database; - - -CREATE TABLE IF NOT EXISTS images ( - id INT AUTO_INCREMENT PRIMARY KEY, - filename VARCHAR(255) NOT NULL, - filepath VARCHAR(255) NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); diff --git a/testing/database_frontend_communication/connect.php b/testing/database_frontend_communication/connect.php deleted file mode 100644 index 4c4b377..0000000 --- a/testing/database_frontend_communication/connect.php +++ /dev/null @@ -1,23 +0,0 @@ -connect_error) { - die("Connection failed: " . $conn->connect_error); -} - -echo "Connected successfully"; - -// Close connection -$conn->close(); - -?> - diff --git a/testing/database_frontend_communication/index.html b/testing/database_frontend_communication/index.html deleted file mode 100644 index bbd02f2..0000000 --- a/testing/database_frontend_communication/index.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - PHP MySQL Database Connection - - - -

PHP MySQL Database Connection

- -
- -

- - -

- - -

- - -

- - -
- - - -