|
1 | 1 | <?php
|
2 |
| -require_once __DIR__ . '/db.php'; |
| 2 | + require_once __DIR__ . '/db.php'; |
3 | 3 |
|
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]; |
11 | 11 |
|
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>"; |
31 | 37 | }
|
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 |
| -} |
36 | 38 | ?>
|
37 | 39 |
|
38 | 40 | <HTML>
|
39 | 41 | <HEAD>
|
40 |
| - <TITLE>PHP | BLOB file upload to MySQL</TITLE> |
| 42 | + <TITLE>PHP | Upload BLOB files in MySQL</TITLE> |
41 | 43 | <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"> |
44 | 46 | <meta name="author" content="JORGE LUIS AGUIRRE MARTINEZ">
|
45 | 47 | <meta name="publish_date" property="og:publish_date" content="2023-03-10T18:00:00-0600">
|
46 | 48 | <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
0 commit comments