Jump to content

Unknown PHP code error


Chikwado

Recommended Posts

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 by Chikwado
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...