Chikwado Posted July 12, 2022 Share Posted July 12, 2022 (edited) Someone help with better explanations on code below. I want to test uploading picture in database using the script below. The code is not showing success or error and I'm unable to figure out what to do. [code] <html> <head> <?php $servername = "server"; $username = "username"; $password = "passme"; $dbname = "mydb"; $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } if(isset($_POST["submit"])) { $name = $_FILES["fileupload"]["name"]; $username = $_POST["username"]; $text = $_POST["textdata"]; $target_dir = "upload/"; $target_file = $target_dir . basename($_FILES["fileupload"]["temp_name"]); // Select file type $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Valid file extensions $extensions_arr = array("jpg","jpeg","png","gif"); // Check extension if( in_array($imageFileType,$extensions_arr) ){ // Upload file if(move_uploaded_file($_FILES['fileupload']["name"],$target_dir.$name)){ // Convert to base64 $image_base64 = base64_encode(file_get_contents('upload/'.$name) ); $image = 'data:image/'.$imageFileType.';base64,'.$image_base64; // prepare and bind $stmt = $conn->prepare("INSERT INTO messagebox (image, username, reg_date, text) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssss", $image, $username, NOW(), $text); $stmt->execute(); echo "<>Success!"; $stmt->close(); $conn->close(); } } } ?> </body> </html> [/code] Edited July 12, 2022 by Chikwado Link to comment Share on other sites More sharing options...
dsonesuk Posted July 12, 2022 Share Posted July 12, 2022 You do realise image converted to base64 is going to be a massive size. The code applied to $image has a semi colon in the middle. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now