Skip to content

Commit 3d1c694

Browse files
committed
Fix show images and some fixes
1 parent b5195bf commit 3d1c694

File tree

4 files changed

+47
-42
lines changed

4 files changed

+47
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ CREATE TABLE `tbl_image` (
8888
</div>
8989
</p>
9090

91-
<p align="center"> Last updated at 10 Mar 2023</p>
91+
<p align="center"> Last updated at 15 Mar 2023</p>

imageView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require_once __DIR__ . '/db.php';
2+
require_once __DIR__ . '/db.php';
33
// Read image BLOB to display
44
// Get image data stored with the MySQL BLOB field in the database
55
if (isset($_GET['image_id'])) {

index.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
<?php
2-
require_once __DIR__ . '/db.php';
2+
require_once __DIR__ . '/db.php';
33

4-
// Get file count in table
5-
$sql = "SELECT COUNT(*) FROM tbl_image";
6-
$stmt = $conn->prepare($sql);
7-
$stmt->execute();
8-
$result = $stmt->get_result();
9-
$row = $result->fetch_row();
10-
// echo $row[0];
4+
// Get file count in table
5+
$sql = "SELECT COUNT(*) FROM tbl_image";
6+
$stmt = $conn->prepare($sql);
7+
$stmt->execute();
8+
$result = $stmt->get_result();
9+
$row = $result->fetch_row();
10+
echo 'Total of registers: '. $row[0];
1111

12-
// Validate the limit of files allowed in the database
13-
if($row[0] < 15){
14-
// Check if $_FILES count items
15-
if (count($_FILES) > 0) {
16-
// Check if file was uploaded
17-
if (is_uploaded_file($_FILES['userImage']['tmp_name'])) {
18-
// Insert image as MySQL BLOB usign file_get_contents() to reads entire file into a string
19-
$imgData = file_get_contents($_FILES['userImage']['tmp_name']);
20-
$imgName = $_FILES['userImage']['name'];
21-
$imgType = $_FILES['userImage']['type'];
22-
$imgSize = $_FILES['userImage']['size'];
23-
$imgTmpName = $_FILES['userImage']['tmp_name'];
24-
$imgError = $_FILES['userImage']['error'];
25-
$imgURL;
26-
$sql = "INSERT INTO tbl_image(imageData, imageName, imageType, imageSize, imageTmpName, imageError, imageURL, upload_on) VALUES(?, ?, ?, ?, ?, ?, ?, NOW())";
27-
$statement = $conn->prepare($sql);
28-
$statement->bind_param('sssssss', $imgData, $imgName, $imgType, $imgSize, $imgTmpName, $imgError, $imgURL);
29-
$current_id = $statement->execute() or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_connect_error());
30-
}
12+
// Validate the limit of files allowed in the database
13+
if($row[0] < 15){
14+
// Check if $_FILES count items
15+
if (count($_FILES) > 0) {
16+
// Check if file was uploaded
17+
if (is_uploaded_file($_FILES['userImage']['tmp_name'])) {
18+
// Insert image as MySQL BLOB usign file_get_contents() to reads entire file into a string
19+
$imgData = file_get_contents($_FILES['userImage']['tmp_name']);
20+
$imgName = $_FILES['userImage']['name'];
21+
$imgType = $_FILES['userImage']['type'];
22+
$imgSize = $_FILES['userImage']['size'];
23+
$imgTmpName = $_FILES['userImage']['tmp_name'];
24+
$imgError = $_FILES['userImage']['error'];
25+
$imgURL;
26+
$sql = "INSERT INTO tbl_image(imageData, imageName, imageType, imageSize, imageTmpName, imageError, imageURL, upload_on) VALUES(?, ?, ?, ?, ?, ?, ?, NOW())";
27+
$statement = $conn->prepare($sql);
28+
$statement->bind_param('sssssss', $imgData, $imgName, $imgType, $imgSize, $imgTmpName, $imgError, $imgURL);
29+
$current_id = $statement->execute() or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_connect_error());
30+
} else {
31+
echo "Sorry, there was an error uploading your file.";
32+
}
33+
}
34+
} else {
35+
echo "<div style='text-align: center; padding: 30px 0 10px 0; font-size: 20px; color: #c0392b'>
36+
The allowed file limit has been reached <br/> Uploading more files is not allowed due to my condition code <br/> Thanks for coming!</div>";
3137
}
32-
}else{
33-
echo "<div style='text-align: center; padding: 30px 0 10px 0; font-size: 20px; color: #c0392b'>
34-
The allowed file limit has been reached <br/> Uploading more files is not allowed due to my condition code <br/> Thanks for coming!</div>";
35-
}
3638
?>
3739

3840
<HTML>
3941
<HEAD>
40-
<TITLE>PHP | BLOB file upload to MySQL</TITLE>
42+
<TITLE>PHP | Upload BLOB files in MySQL</TITLE>
4143
<meta charset="UTF-8">
42-
<meta name="description" content="Example MySQL BLOB using PHP">
43-
<meta name="keywords" content="PHP, MySQL, BLOB">
44+
<meta name="description" content="Example of upload BLOB files in MySQL using PHP">
45+
<meta name="keywords" content="PHP, MySQL, BLOB, Files, Upload">
4446
<meta name="author" content="JORGE LUIS AGUIRRE MARTINEZ">
4547
<meta name="publish_date" property="og:publish_date" content="2023-03-10T18:00:00-0600">
4648
<meta name="viewport" content="width=device-width, initial-scale=1.0">

listImages.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
<?php
22
// Gets all image id in the database
3-
$sql = "SELECT imageId FROM tbl_image ORDER BY imageId DESC";
3+
$sql = "SELECT imageId, imageData FROM tbl_image ORDER BY imageId DESC";
44
$stmt = $conn->prepare($sql);
55
$stmt->execute();
66
$result = $stmt->get_result();
77
?>
88

99
<?php
10-
// Show all images from the database one by one
10+
// Show all images from the database one by one for reducing overcharge
1111
if ($result->num_rows > 0) {
1212
while ($row = $result->fetch_assoc()) {
13+
// Show only images that contain imageData in the database
14+
if($row["imageData"]){
1315
?>
14-
<?php ?>
15-
<!--- Display uploaded blob images in a gallery --->
16-
<img src="imageView.php?image_id=<?php echo $row["imageId"];?>">
17-
<?php
16+
<?php ?>
17+
<!--- Display uploaded blob images in a gallery --->
18+
<img src="imageView.php?image_id=<?php echo $row["imageId"];?>">
19+
<?php
20+
}
1821
}
1922
}
2023
?>

0 commit comments

Comments
 (0)