added php database functionality to upload images
This commit is contained in:
parent
65528d31cc
commit
0960ef3219
41
backend/docker-compose.yml
Normal file
41
backend/docker-compose.yml
Normal file
|
@ -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
|
66
backend/php/index.php
Executable file
66
backend/php/index.php
Executable file
|
@ -0,0 +1,66 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Upload Image</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
|
||||||
|
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
|
||||||
|
Select image to upload:
|
||||||
|
<input type="file" name="image" id="image">
|
||||||
|
<input type="submit" value="Upload Image" name="submit">
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$servername = "172.20.0.5";
|
||||||
|
$username = "root";
|
||||||
|
$password = "root";
|
||||||
|
|
||||||
|
$mysqli = new mysqli($servername, $username, $password);
|
||||||
|
|
||||||
|
if ($mysqli->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
|
@ -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
|
|
||||||
);
|
|
|
@ -1,23 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
// Retrieve form data
|
|
||||||
$hostname = $_POST['hostname'];
|
|
||||||
$username = $_POST['username'];
|
|
||||||
$password = $_POST['password'];
|
|
||||||
$database = $_POST['database'];
|
|
||||||
|
|
||||||
// Attempt MySQL server connection
|
|
||||||
$conn = new mysqli($hostname, $username, $password, $database);
|
|
||||||
|
|
||||||
// Check connection
|
|
||||||
if ($conn->connect_error) {
|
|
||||||
die("Connection failed: " . $conn->connect_error);
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Connected successfully";
|
|
||||||
|
|
||||||
// Close connection
|
|
||||||
$conn->close();
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>PHP MySQL Database Connection</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<h2>PHP MySQL Database Connection</h2>
|
|
||||||
|
|
||||||
<form action="connect.php" method="post">
|
|
||||||
<label for="hostname">Hostname:</label>
|
|
||||||
<input type="text" id="hostname" name="hostname" required><br><br>
|
|
||||||
|
|
||||||
<label for="username">Username:</label>
|
|
||||||
<input type="text" id="username" name="username" required><br><br>
|
|
||||||
|
|
||||||
<label for="password">Password:</label>
|
|
||||||
<input type="password" id="password" name="password" required><br><br>
|
|
||||||
|
|
||||||
<label for="database">Database:</label>
|
|
||||||
<input type="text" id="database" name="database" required><br><br>
|
|
||||||
|
|
||||||
<input type="submit" value="Connect">
|
|
||||||
</form>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
Loading…
Reference in a new issue