0

The image is being corrupted when uploaded to the database so i tried to do the base64_encode but it didn't work any suggestions as to what i can do ? its a school project and its due 2 more days hope you can help :/ thx and appreciate the help.

<?php

session_start();

include_once('../includes/connection.php');

if(isset($_SESSION['logged_in'])){
if(isset($_POST['title'], $_POST['content'], $_POST['image'] )){
    $title = $_POST['title'];
    $image = $_POST['image'];
    $image = base64_encode($image); 
    $content = nl2br($_POST['content']);

    if(empty($title) or empty($content) or empty($content)){
        $error = 'All fields are required';
    } else{
        $query = $pdo->prepare('INSERT INTO articles (article_title, article_content, picture_upload) VALUES (?, ?, ?)');

        $query->bindValue(1, $title);
        $query->bindValue(2, $content);
        $query->bindValue(3, $image);

        $query->execute();

        header('Location: index.php');
    }
}
?>
<html>
<head>
    <title>CMS</title>
    <link rel="stylesheet" href="../assets/style.css"
</head>

<body>
    <div class="container">
        <a href="index.php" id="logo">CMS</a>       
        <br />
        <h4>Add Article</h4>
        <?php if(isset($error)){ ?>
            <small style="color:#aa0000;"><?php echo $error;?> </small>
        <?php } ?>
        <br /><br />
        <form action="add.php" method="post" autocomplete="off">
            <input type="text" name="title" placeholder="Title" />
            <br /><br /> <input type="file" name="image" id="file"><br><br />
            <textarea rows="15" cols="50" placeholder="Content" name="content"></textarea>
            <input type="submit" value="Submit Article" />
        </form>
    </div>
</body>
<?php
} else{
header('Location index.php');
}

?>
  • How exactly is it corrupted? What have you tried to solve this problem? Why do you even want to save an image in the db, why not store it on the server and just save the url? – kero Sep 15 '13 at 20:57
  • 1
    The image should be in $_FILES and not $_POST... Your form is incomplete too. It needs an enctype="multipart/form-data" I think. And I suggest doing what kingkero is suggesting if you just want to get done with it. – Touch Sep 15 '13 at 20:58
  • I cant do that kingkero cause its a cms project and they want the admin feature and yes i fixed the $_FILES and the form its still doesnt work – Owen Muscat Sep 15 '13 at 21:08
  • @OwenMuscat The use of `enctype="multipart/form-data"` is a must for both file attachments and uploading files. Add that and try it again. – Funk Forty Niner Sep 15 '13 at 21:08
  • Can you pls explain a bit @Fred -ii- – Owen Muscat Sep 15 '13 at 21:11
  • update your question with your current code –  Sep 15 '13 at 21:12
  • You should not store binary image data into a database. Just upload the file and save path and filename (+additional information if needed [width, height, orientation, ...]) to the database. – djot Sep 15 '13 at 21:15
  • @OwenMuscat [This will explain it better](http://stackoverflow.com/a/4526286/1415724) and [this also](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2) – Funk Forty Niner Sep 15 '13 at 21:22
  • @OwenMuscat ^-- should you ever want to know more about `enctype="multipart/form-data"` – Funk Forty Niner Sep 15 '13 at 21:30
  • @OwenMuscat prefer if you didn't swear on this site. –  Sep 15 '13 at 22:04

0 Answers0