Last active
March 7, 2021 08:06
-
-
Save okayshankha/3cde35eeba5cdc3c990ea4d3eba12632 to your computer and use it in GitHub Desktop.
To Store Pictures in MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// DATABASE SETTINGS // CHANGE THESE TO YOUR OWN !! | |
define('DB_HOST', 'localhost'); | |
define('DB_NAME', 'computopedia_test_db'); | |
define('DB_CHARSET', 'utf8'); | |
define('DB_USER', 'computopedia'); | |
define('DB_PASSWORD', ''); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `upload`( | |
`NAME` varchar(255) NOT NULL, | |
`data` longblob NOT NULL | |
)engine=innodb DEFAULT charset=utf8; | |
ALTER TABLE `upload` ADD UNIQUE KEY `NAME` (`NAME`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* [INIT] */ | |
require "config.php"; | |
/* [CONNECT TO DB] */ | |
$pdo = new PDO( | |
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET, | |
DB_USER, DB_PASSWORD, | |
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, | |
PDO::ATTR_EMULATE_PREPARES => false] | |
); | |
/* [FETCH IMAGE] */ | |
$stmt = $pdo->prepare("SELECT `data` FROM `upload` WHERE `name`=?"); | |
$stmt->execute(array($_GET['f'])); | |
$img = $stmt->fetch(); | |
/* [OUTPUT] */ | |
// DO MORE ERROR CHECKING & HANDLING HERE IF YOU WANT | |
// NOT FOUND - SHOW BLANK IMAGE | |
if ($img==false) { | |
require "404.jpg"; | |
} else { | |
echo $img['data']; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Image Upload</title> | |
</head> | |
<body> | |
<form action="upload.php" method="post" enctype="multipart/form-data"> | |
<input type="file" name="upFile" id="upFile" accept=".png,.gif,.jpg,.webp" required> | |
<input type="submit" name="submit" value="Upload Image"> </form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Show Image</title> | |
</head> | |
<body> | |
<!-- !! REMEMBER TO CHANGE ?f=IMAGE TO YOUR UPLOADED IMAGE FILE NAME !! --> | |
<img src="fetch.php?f=image.jpg"> </body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* [INIT] */ | |
require "config.php"; | |
/* [ERROR CHECKING] */ | |
if ($_FILES['upFile']['size']==0) { | |
die("No file selected"); | |
} | |
if (exif_imagetype($_FILES['upFile']['tmp_name'])===false) { | |
die("Not an image"); | |
} | |
/* [CONNECT TO DB] */ | |
$pdo = new PDO( "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET, | |
DB_USER, | |
DB_PASSWORD, | |
[ | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, | |
PDO::ATTR_EMULATE_PREPARES => false | |
] | |
); | |
/* [INSERT IMAGE] */ | |
// DO MORE ERROR CHECKING & HANDLING HERE IF YOU WANT | |
try { | |
$stmt = $pdo->prepare("INSERT INTO `upload` (`name`, `data`) VALUES (?, ?)"); | |
$stmt->execute([$_FILES["upFile"]["name"], file_get_contents($_FILES['upFile']['tmp_name'])]); | |
} catch (Exception $ex) { | |
echo "ERROR - " . $ex->getMessage(); | |
die(); | |
} | |
/* [ON COMPLETE] */ | |
// DO SOMETHING, MAYBE REDIRECT THE USER TO ANOTHER PAGE | |
// header("Location: http://example.com/abc.php"); | |
echo "success"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment